Where is this leaking? / Why am I having memory issues?
I'm having a bit of trouble with an iPad app I'm creating. There is a simple image sequence/animation of about 80 frames at one point.
The code looks like this (this is a UIView subclass subclass):
- (id)init {
UIImage *theImage = [UIImage imageNamed:@"chart0075.jpg"];
// get the frame
CGRect lgeFrame = CGRectMake(20, 130, theImage.size.width, theImage.size.height);
// set the new frame
CGFloat newHeight = theImage.size.height/1.65;
CGFloat newWidth = theImage.size.width/1.65;
CGRect smlFrame = CGRectMake(480, 200, newWidth, newHeight);
self = [super initWithLargeFrame:lgeFrame smallFrame:smlFrame];
if(self){
// lets add the image as an image view
theImageView = [[UIImageView alloc] initWithImage:theImage];
[theImageView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[theImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:theImageView];
// now we need to make an array of images for the image sequence
NSMutableArray *imageSeq = [NSMutableArray array];
for(int i = 1; i < 76; i++){
NSString *jpgnumber;
if(i<10){
jpgnumber = [NSString stringWithFormat:@"000%i",i];
}
else {
jpgnumber = [NSString stringWithFormat:@"00%i",i];
}
NSString *imageFile = [[NSBundl开发者_开发问答e mainBundle] pathForResource:[NSString stringWithFormat:@"chart%@",jpgnumber] ofType:@"jpg"];
[imageSeq addObject:[UIImage imageWithContentsOfFile:imageFile]];
}
[theImageView setAnimationImages:imageSeq];
[theImageView setAnimationDuration:1.5];
[theImageView setAnimationRepeatCount:1];
}
return self;
}
Then on a reverse pinch gesture the image is supposed to animate. The first time I do this reverse pinch gesture it takes a few seconds to start the animation. And sometimes I get a memory warning level 1 and the app will crash.
Whats the problem here? Are 80 jpgs too much to keep in memory at once? They're well under 2mb big in total, so they surely shouldn't be filling up the ipad's memory right?
I've looked at it with the allocations tool which is suggesting that I have about 40kb in memory at the time of the animation, but then this goes back down to 0 during subsequent animations. (although the allocations tool does confuse me quite a bit).
Does anyone have any idea what's causing this? I can post more code or anything if necessary?
Thanks a lot :)
Your memory usage depends on how big the images are uncompressed. The width times the height time 4 will tell you the number of bytes the images will take each, multiply by the number of images to get the total.
My guess is you are on the edge of being over memory wise.
Run in Instruments with the VMTracker instrument to be sure. You should be looking at the Dirty Resident memory.
WWDC '10 and WWDC '09 both had great content on Instruments and memory usage analysis.
You are not releasing theImageView
[theImageView release]
精彩评论