【发布时间】:2011-07-24 14:53:46
【问题描述】:
我在创建 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 jpg 是否太多,无法一次记住?它们总共不到 2mb,所以它们肯定不应该填满 ipad 的内存吧?
我用分配工具查看了它,这表明我在动画时有大约 40kb 的内存,但在随后的动画中这又回到了 0。 (虽然分配工具确实让我很困惑)。
有人知道是什么原因造成的吗?如果需要,我可以发布更多代码或任何内容?
非常感谢:)
【问题讨论】:
-
我不知道它是否有帮助,但我会将
NSString *jpgnumber;和NSString *imageFile;移到 for 循环 See here 之外。另外,我会使用NSMutableArray *imageSeq = [[NSMutableArray alloc] init];。只要有可能,我就会避免使用自动释放的对象。 -
您好。我已经使我的可变数组和图像非自动释放,并且我已经将所有指针移到了 for 循环之外,但我仍然遇到同样的问题...... :( 感谢您的帮助。
标签: objective-c cocoa-touch ipad memory-management memory-leaks