Reputation: 1
when I doing animation with the next code:
NSArray *images=[NSArray arrayWithObjects:[UIImage imageNamed:@"shavit_1.png"],[UIImage imageNamed:@"shavit_1.png"],[UIImage imageNamed:@"shavit_2.png"],[UIImage imageNamed:@"shavit_3.png"],[UIImage imageNamed:@"shavit_4.png"],[UIImage imageNamed:@"shavit_5.png"],[UIImage imageNamed:@"shavit_6.png"],[UIImage imageNamed:@"shavit_7.png"],[UIImage imageNamed:@"shavit_8.png"],[UIImage imageNamed:@"shavit_9.png"],[UIImage imageNamed:@"shavit_10.png"],[UIImage imageNamed:@"shavit_11.png"],[UIImage imageNamed:@"shavit_12.png"],[UIImage imageNamed:@"shavit_13.png"],[UIImage imageNamed:@"shavit_14.png"],[UIImage imageNamed:@"shavit_15.png"],[UIImage imageNamed:@"shavit_16.png"],[UIImage imageNamed:@"shavit_17.png"],[UIImage imageNamed:@"shavit_18.png"],[UIImage imageNamed:@"shavit_19.png"],[UIImage imageNamed:@"shavit_20.png"],[UIImage imageNamed:@"shavit_21.png"],[UIImage imageNamed:@"shavit_22.png"],[UIImage imageNamed:@"shavit_23.png"],[UIImage imageNamed:@"shavit_24.png"],[UIImage imageNamed:@"shavit_25.png"],nil];
shavitimage.animationImages=images;
shavitimage.animationDuration=2.0;
shavitimage.animationRepeatCount=1;
[shavitimage startAnimating];
[images release]; //release the used array
[self performSelector:@selector(animation_ends) withObject:nil afterDelay:2.0];//after 2 seconds put astatic image
timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
}
-(void)animation_ends{
[shavitimage stopAnimating];//the UIImageView return to static mode
shavitimage.image=[UIImage imageNamed:@"shavit_25.png"];
self.view.userInteractionEnabled=YES;
}
the animation is working well,and the application working well after the end of the animation. but when I doing animation with the next code:
NSMutableArray *images=[NSMutableArray array]; //insrt the images dynamiclly
for(int i=1;i<=25;i++)
{
NSString *file=@"shavit_";
file=[file stringByAppendingFormat:@"%d",i];
NSString *filePath = [[NSBundle mainBundle] pathForResource:file ofType:@"png"];
[images addObject:[UIImage imageWithContentsOfFile:filePath]];
[file release];
[filePath release];
}
shavitimage.animationImages=images;
shavitimage.animationDuration=2.0;
shavitimage.animationRepeatCount=1;
[shavitimage startAnimating];
[images release]; //release the used array
[self performSelector:@selector(animation_ends) withObject:nil afterDelay:2.0];//after 2 seconds put astatic image
timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
}
-(void)animation_ends{
[shavitimage stopAnimating];//the UIImageView return to static mode
shavitimage.image=[UIImage imageNamed:@"shavit_25.png"];
self.view.userInteractionEnabled=YES;
}
the animation is working well,but it stack after.why does it happen?
Upvotes: 0
Views: 6270
Reputation: 351
@synthesize myImage;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableArray *a = [NSMutableArray array];
//[a addObject: [UIViewController ]];
[a addObject:[UIImage imageNamed:@"1.gif"]];
[a addObject:[UIImage imageNamed:@"2.gif"]];
[a addObject:[UIImage imageNamed:@"3.gif"]];
[a addObject:[UIImage imageNamed:@"4.gif"]];
[a addObject:[UIImage imageNamed:@"5.gif"]];
[a addObject:[UIImage imageNamed:@"6.gif"]];
[a addObject:[UIImage imageNamed:@"7.gif"]];
//[myImage initWithFrame:CGRectMake(0, 0, 131, 125)];
myImage.animationImages = a;
myImage.animationDuration=5;
myImage.animationRepeatCount=0;
myImage.startAnimating;
[self.view addSubview:myImage];
}
Upvotes: 0
Reputation: 44633
Do not release an autoreleased object.
That's the gist of it.
Evaluation of chain of events :–
Retain Count = 1 (will diminish by 1 in near future)
UIImageView
object retains the array when you set animationImages
. Retain Count = 2
Retain Count = 1
Retain Count = 0 (object is deallocated)
Now the UIImageView
still thinks that it has ownership of the array. When it tries to access it then it should result in an error. If it isn't animating and then is later deallocated, it will try to send a release
message to the deallocated instance which should also crash the application. All of this is fair assumption as we don't know if the framework is adding its own memory management calls in any way.
Now both the pieces of code have the same problem and are both unreliable.
Be a good memory citizen and remove the release
call.
Upvotes: 1