【发布时间】:2011-05-09 21:14:10
【问题描述】:
我有一个奇怪的问题,但我很确定,对你来说不是。如果是这样,请帮助我或解释。 我有一个包含 39 个 UIImages 的数组,它们将用于为 UIImageView 设置动画。并且播放动画后,内存没有释放。
- (void)viewDidLoad {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSMutableArray* actionArray = [[NSMutableArray alloc]initWithCapacity:38];
for (int i=1; i<=39; i++) {
NSString* path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"bg_Level1_%.4d", i] ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
[path release];
[actionArray addObject:image];
[image release];
NSLog(@"path rc %d image %d", [path retainCount], [image retainCount]);
}
imageView.animationImages = actionArray;
imageView.animationDuration = 4.0;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
NSLog(@"RetainCount ActArr %d ImageView %d", [actionArray retainCount], [imageView retainCount]);
[actionArray release];
[pool drain];
[imageView release];
[self performSelector:@selector(playAnimation2) withObject:self afterDelay:5.0];
[super viewDidLoad];
}
-(void)playAnimation2{
if ([imageView isAnimating]) {
[imageView stopAnimating];
}
NSLog(@"RetainCount ImageView %d",[imageView retainCount]);
}
没有发现泄漏,但分配的内存量仍然是 24 mb。
还是没那么必要?
编辑:
对不起,我的错。 由于我的代码的许多更改,我迷失了它。分析后我明白了! 非常感谢!
正确的代码是:
- (void)viewDidLoad {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSMutableArray* actionArray = [[NSMutableArray alloc]initWithCapacity:38];
for (int i=1; i<=39; i++) {
NSString* path = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"bg_Level1_%.4d", i] ofType:@"png"];
UIImage *image = [[UIImage alloc]initWithContentsOfFile:path];
// [path release];
[actionArray addObject:image];
[image release];
NSLog(@"path rc %d image %d", [path retainCount], [image retainCount]);
}
imageView.animationImages = actionArray;
imageView.animationDuration = 4.0;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
NSLog(@"RetainCount ActArr %d ImageView %d", [actionArray retainCount], [imageView retainCount]);
[actionArray release];
[pool drain];
[self performSelector:@selector(playAnimation2) withObject:self afterDelay:5.0];
[super viewDidLoad];
}
-(void)playAnimation2{
if ([imageView isAnimating]) {
[imageView stopAnimating];
}
[imageView removeFromSuperview];
imageView.animationImages = nil;
NSLog(@"RetainCount ImageView %d",[imageView retainCount]);
}
问题出在[path release];
【问题讨论】:
-
-1 因为您的代码又一次完全不起作用了。它会立即崩溃,因为您过度释放 path。显示实际代码。编辑:好的,它不会直接崩溃,但它会在图像释放时崩溃。
-
我怎样才能把整个项目发给你?最有趣的是,没有任何崩溃。
-
当然它不会崩溃,因为你的图片没有发布 - 因此你的问题。
-
releasing problems 的可能重复项
-
如果添加 39 张图片,您可能希望从至少 39 张容量开始。
标签: iphone objective-c memory-management