Hi there, I am working on an iPhone app at the moment and I am struggling with some animation.
Basically I have 109 frames for dragging about a man on screen, so what I am doing is listening for touches, and calculating which frames I need to switch to.
This works all well and good, but after a while I get a memory leak and the app crashes. I am loading all the images into and Array at startup, and using a UIImageView to display the images. Images are loaded using imageWithContentsOfFile.
What is the best way for me to do this??
Heres some of the source code:
- (void)viewWillAppear:(BOOL)animated {
animationQueue = [[NSMutableArray alloc] initWithObjects:0];
imageArray = [[NSMutableArray alloc] initWithObjects:nil];
for(int i = 1;i<110;i++)
{
[imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"%i", i] ofType:@"png"]]];
}
[super viewWillAppear:animated];}
View Did Load code:
- (void)viewDidLoad {
animation = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 480, 320)];
animation.contentMode = UIViewContentModeCenter;
[self.view addSubview:animation];
animation.image = [imageArray objectAtIndex:40];
[super viewDidLoad];}
Touches Handler:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// Enumerates through all touch objects
for (UITouch *touch in touches) {
CGPoint touch_point = [touch locationInView:self.view];
// calculate which frame to end on
int pos = ceil((touch_point.x/480) * 108);
[self redrawAnimation:pos];
}}
Redrawing Animation:
- (void)redrawAnimation:(int)end_frame {
animation.image = [imageArray objectAtIndex:end_frame];
}
The application crashes when you have been sliding your finger along the screen a few times.
Thanks in advance!
Matt