iphone UIImage array memory leak - iphone

I had that question before, tried as answered, but still it does not work.
Before I allocated with:
imageArray_danceright = [[NSArray alloc] initWithObjects:
I came adviced to do like bellow, but now it crash immediatly after second anim is called:
//init areas:
imageArray_stand = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankieSingtRetime_0001" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankieSingtRetime_0002" ofType:#"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0003" ofType:#"jpg"]],nil];
imageArray_danceleft = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0001" ofType:#"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0040" ofType:#"jpg"]],nil];
imageArray_danceright = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0041" ofType:#"jpg"]],
.. 40 images
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"FrankiePeaceRetime_0080" ofType:#"jpg"]],nil];
//start:
[myimageview setAnimationImages:imageArray_stand];
myimageview.animationDuration = 0.23;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0.0;
myimageview.image = [myimageview.animationImages objectAtIndex:1];
[myimageview startAnimating];
// ------ in my 'touchesBegan" i have:
if ([touch view] == overlay_fuesserechts) {
[myimageview.animationImages release];
[myimageview setAnimationImages:imageArray_danceright];
myimageview.animationDuration = 2.0;
myimageview.contentMode = UIViewContentModeBottomLeft;
myimageview.animationRepeatCount = 0;
[myimageview startAnimating];
}
- (void)dealloc {
[imageArray_stand release];
imageArray_stand=nil;
[imageArray_danceright release];
imageArray_danceright=nil;
[imageArray_danceleft release];
imageArray_danceleft=nil;
super dealloc];
}
Its starts with the "stand" animation.. but when i fire touchesbegan it crashes immediatly at:
[myimageview setAnimationImages:imageArray_danceright];
and i have no idea. I tried so many things. Now I hope you have an idea.
thx
chris

Because you are autoreleasing your image arrays, by the time it gets to TouchesBegan they have already been released.
Who told you to use arrayWithObjects instead of alloc - initWithObjects ?
It sounds like your earlier version would have been more correct. What was the problem you were having with that?

You surely don't need the [myimageview.animationImages release]; at the beginning of touches began. You are creating autoreleased array objects and assigening them to the imageview which releases them.

Related

App getting crash in background image loading iphone

I have scroll view with 60 UIImageView's. Images displaying in these imageviews are from url and url's i get from the webservice. When user scrolls to bottom I call the webservice and get new 60 urls. After getting the url i am utilizing same UIImageView's to display images. Following is my code for displaying images.
for (UIView *viewSel in [scrlView subviews]) {
NSString *strImgUrl = [arrImgURL valueForKey:#"url"];
if ([viewSel isKindOfClass:[UIImageView class]]) {
UIImageView *imgView = (UIImageView *)viewSel;
[imgView setImage:[UIImage imageNamed:#"loading432x520.png"]];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:[NSString stringWithFormat:#"%#",strImgUrl]];
[arr addObject:imgView];
[self performSelectorInBackground:#selector(loadImageInBackground:) withObject:arr];
[arr release];
}
}
- (void) loadImageInBackground:(NSMutableArray *)arr {
NSLog(#"loadImage");
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[arr objectAtIndex:0]]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
if (img != nil) {
[arr addObject:img];
}
else{
[arr addObject:[UIImage imageNamed:#"no-image432x520.png"]];
}
[img release];
[self performSelectorOnMainThread:#selector(assignImageToImageView:) withObject:arr waitUntilDone:YES];
[pool release];
}
- (void) assignImageToImageView:(NSMutableArray *)arr{
NSLog(#"assignImage");
UIImageView *imgView = [arr objectAtIndex:1];
imgView.image = [arr objectAtIndex:2];
}
The code works perfect for first time. But when i get new urls it is working some time or getting crash. I don't know why it is getting crash. I want to stop that crash. If you are not getting to my question then let me know. Please help me for this. Thanks in advance. Valid answer will be appreciated.
Thank you all to give your help full comments to my question. I got the error. I am adding CALayer to uiimageview when allocating memory to uiimageview. I remove that CALayer code and it work perfectly. Nikita's comment help me to find my error.

Can't tweet an image

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
forTweet = TRUE;
switch(buttonIndex)
{
case 0:
{
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
// Set the initial tweet text. See the framework for additional properties that can be set.
NSMutableString * twitterString = [displayString mutableCopy];
[twitterString appendString: #" #LoviOtvet"];
[tweetViewController setInitialText: twitterString];
UIImage * imageAnswer = [self getImageFromURL: [self strToUrlConverter]];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
[tweetViewController addImage: [self mergeImage:imageAnswer withImage:imageLogo strength:1]];
break;
}
I added . No errors, but not working.
Facebook and saving to device working. Tweeting not. Why not?
Your problem is this line:
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile: #"image.png"];
For this method, you would need to need to provide a file path, not a name. You have two choices:
UIImage * imageLogo = [UIImage imageNamed:]
or (for a file in the bundle):
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
UIImage * imageLogo = [[UIImage alloc] initWithContentsOfFile:path];

UIImageView animation after action?

I have a problem setting up an animation. The first imageview starts animating and when playsound1 is called then starts the second animation. Everything works fine but right now when the second animation stops there is no animation going on. So what I want to do is after the second animation stops - the first to start all over-- then again when method is called to play the second animation . Any hints?
Here you can see a part of the code as it is right now:
- (void)loadtest1 {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"test1.png"],
[UIImage imageNamed:#"test2.png"],
[UIImage imageNamed:#"test3.png"],
[UIImage imageNamed:#"test4.png"],
nil];
test1.animationImages = imageArray;
test1.animationRepeatCount = 0;
test1.animationDuration = 1;
[imageArray release];
[test1 startAnimating];
}
- (void)loadtest2 {
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"test4.png"],
[UIImage imageNamed:#"test5.png"],
[UIImage imageNamed:#"test6.png"],
[UIImage imageNamed:#"test7.png"],
nil];
test2.animationImages = imageArray;
test2.animationRepeatCount = -1;
test2.animationDuration = 1;
[imageArray release];
[test2 startAnimating];
}
- (IBAction)playsound1 {
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
test1.hidden = 0;
test2.hidden = 1;
[test1 startAnimating];
test2.center = test1.center;
}
If I understand you correctly, what you are trying to do is re-starting your first animation when the second one finishes
The correct way to handle this is through a CABasicAnimation, which allows you to specify a delegate whose – animationDidStop:finished: method is called when the animation is done.
If you do not want this way, a sort of workaround is the following: in loadtest2, after you start the animation, schedule a method for execution with a delay:
[self performSelector:#selector(loadTest1) withObject:nil afterDelay:1.0];
this will (more or less) work satisfactorily because you know how long your animation will last. So, when the animation should be ended, loadTest1 is executed and the first animation starts again.

UIImageView doesn't animate

I have the following code inside my app, but it doesn't animate the UIImageView, can anyone tell me why?
UITableViewCell* cell = [selectedTable cellForRowAtIndexPath:selectedPath];
NSArray* myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked117x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked217x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked317x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked417x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked517x11" ofType:#"png"]],
[UIImage imageNamed:[[NSBundle mainBundle] pathForResource:#"boxBeingChecked617x11" ofType:#"png"]],
nil];
cell.imageView.animationImages = myImageArray;
cell.imageView.animationDuration = 4.20;
cell.imageView.animationRepeatCount = 1;
[cell.imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:4.0
target:self
selector:#selector(methodToInvokeAfterAnimationIsDone)
userInfo:nil
repeats:NO];
You should use this:
NSArray* myImageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"boxBeingChecked117x11.png"]],
[UIImage imageNamed:#"boxBeingChecked217x11.png"]],
[UIImage imageNamed:#"boxBeingChecked317x11.png"]],
[UIImage imageNamed:#"boxBeingChecked417x11.png"]],
[UIImage imageNamed:#"boxBeingChecked517x11.png"]],
[UIImage imageNamed:#"boxBeingChecked617x11.png"]],
nil];
You need to give name of the file, not the path, in imageNamed: method.
Make sure that the imageView's highlighted property is set to NO, otherwise the code you have above will not work. If you want to animate highlighted images, use highlightedAnimationImages.

Retrieving the UIImage of a TTPhotoViewController

I have a TTPhotoViewController subclass working nicely. I need to be able to give the option of saving the image downloaded to the user's saved photos directory. I am having problems actually retrieving a UIImage object.
After looking through the API I have seen that TTPhoto (which is one of the properties of the view controller) doesn't actually contain a UIImage to use. However, within the API there is a TTImageView class which has a UIImage property. TTPhotoView derives from TTImageView and there is one of these in my view controller class called "PhotoStatusView". However when I access this I do not get a UIImage back.
Any ideas?
Check this site out add save to album functionality
Here is the code from the site:
_clickActionItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAction
//TTIMAGE(#"UIBarButtonReply.png")
target:self action:#selector(clickActionItem)];
UIBarButtonItem* playButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemPlay target:self action:#selector(playAction)] autorelease];
playButton.tag = 1;
UIBarItem* space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
_toolbar = [[UIToolbar alloc] initWithFrame:
CGRectMake(0, screenFrame.size.height - TT_ROW_HEIGHT,
screenFrame.size.width, TT_ROW_HEIGHT)];
_toolbar.barStyle = self.navigationBarStyle;
_toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleTopMargin;
NSString *searchCaption = #"Photo from networked";
NSRange range = [[_centerPhoto caption] rangeOfString:searchCaption];
if (range.location == NSNotFound) {
_toolbar.items = [NSArray arrayWithObjects:
space, _previousButton, space, _nextButton, space,_clickActionItem, nil];
[_innerView addSubview:_toolbar];
}else{
_toolbar.items = [NSArray arrayWithObjects:
space, _previousButton, space, _nextButton, space, nil, nil];
[_innerView addSubview:_toolbar];
}
and this
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(6 == actionSheet.tag)
{
if(0 == buttonIndex)//save page
{
NSLog(#"photo: %#", [_centerPhoto URLForVersion:TTPhotoVersionLarge]);
NSURL *aUrl = [NSURL URLWithString:[_centerPhoto URLForVersion:TTPhotoVersionLarge]];
NSData *data = [NSData dataWithContentsOfURL:aUrl];
UIImage *img = [[UIImage alloc] initWithData:data];
NSLog(#"photo:class %#", [img class]);
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
You should try using:
UIImage *image = [[TTURLCache sharedCache] imageForURL:URL];