我想了解自動引用計數,因爲我來自高級編程語言(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