我在創建一款iPad應用程序時遇到了一些麻煩。有一個簡單的圖像序列/動畫約80幀在一個點。這個泄漏在哪裏? /爲什麼我有內存問題?
的代碼看起來是這樣的(這是一個UIView子類的子類):
- (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 = [[NSBundle 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;
}
然後在一個反向捏合手勢的圖像應該動畫。我第一次做這個反向捏手勢需要幾秒鐘來開始動畫。有時我得到一個內存警告級別1,該應用程序將崩潰。
這裏有什麼問題?一次80馬力的記憶太多了嗎?他們總共大大低於2MB,所以他們肯定不應該填滿iPad的記憶嗎?
我已經看到它與分配工具,這是建議我在動畫時在內存中約40kb,但隨後這回落到0在隨後的動畫。 (儘管分配工具確實讓我感到困惑)。
有沒有人有任何想法是什麼造成這個?我可以發佈更多的代碼或任何必要的東西?
感謝很多:)
我不知道它是否有幫助,但我會移動'NSString * jpgnumber;'和'NSString * imageFile;'在for循環之外[見這裏](http://stackoverflow.com/questions/5052443) /指針和 - 循環)。此外,我會使用使用'NSMutableArray * imageSeq = [[NSMutableArray alloc] init];'。只要有可能,我就避免使用自動釋放的對象。 – fabian789 2011-03-28 16:10:12
你好。我已經讓我的可變數組和圖像非自動發佈,並且我已經將for循環之外的所有指針移動了,但是我仍然遇到了同樣的問題...... :(感謝您的幫助 – 2011-03-28 16:15:33