我想了解自动引用计数,因为我来自高级编程语言(Python),而且我正在使用Objective-C的这一特性的项目。我经常遇到ARC解除分配我以后需要的对象的问题,但现在我有一个具体的例子,我希望我能得到一个解释。为什么我的NSArray被释放?
- (void) animateGun:(UIImageView *)gun withFilmStrip:(UIImage *)filmstrip{
NSMutableArray *frames = [[NSMutableArray alloc] init];
NSInteger framesno = filmstrip.size.width/gun_width;
for (int x=0; x<framesno; x++){
CGImageRef cFrame = CGImageCreateWithImageInRect(filmstrip.CGImage, CGRectMake(x * gun_width, 0, gun_width, gun_height));
[frames addObject:[UIImage imageWithCGImage:cFrame]];
CGImageRelease(cFrame);
}
gun.image = [frames objectAtIndex:0];
gun.animationImages = frames;
gun.animationDuration = .8;
gun.animationRepeatCount = 1;
[gun startAnimating];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(arc4random() % 300)/100 * NSEC_PER_SEC), dispatch_get_current_queue(),^{
[self animateGun:leftGun withFilmStrip:[self getFilmStripForAction:gunShoot andTeam:nil withWeapon:nil]];
});
}
这段代码的代码背后的想法很简单:我有一个(UIImageView*)gun
我与存储在(NSMutableArray *)frames
,在随机时间的图像动画。 (UIImage *)filmstrip
只是一个包含所有将用于动画的帧的图像。动画作品的第一次迭代,但问题出现在第二次迭代中,我得到-[UIImage _isResizable]: message sent to deallocated instance ...
或-[UIImage _contentStretchInPixels]: message sent to deallocated instance ...
或-[NSArrayI release]: message sent to deallocated instance ...
。这发生在
gun.animationImages = frames;
但我不明白为什么。我并没有要求解决我的问题,而只是为了帮助我理解这里发生的事情。谢谢。
代码你如何调用这个函数?多次被称为? –
我第一次用'[self animateGun:leftGun withFilmStrip:[self getFilmStripForAction:gunShoot andTeam:nil withWeapon:nil]]来调用它;'然后我离开'dispatch_after'完成它的工作。 – ov1d1u
对我来说,似乎UIImageView和UIImage引用是问题,而且错误在于此方法之外。 – Mike