一个快速的概念问题,
如果我正在使用 UIImagePickerController,并且我没有实现 didFinishPickingMediaWithInfo,或者在“拍摄”图片后尝试处理返回的 UIImage,那么在委托调用期间将返回的 UIImage 数据会发生什么,它只是由系统?
经过测试,它似乎没有泄漏或被添加到标准照片库中。
谢谢,
一个快速的概念问题,
如果我正在使用 UIImagePickerController,并且我没有实现 didFinishPickingMediaWithInfo,或者在“拍摄”图片后尝试处理返回的 UIImage,那么在委托调用期间将返回的 UIImage 数据会发生什么,它只是由系统?
经过测试,它似乎没有泄漏或被添加到标准照片库中。
谢谢,
我不明白为什么它会首先泄漏。
您必须认为 Apple 开发人员编写的代码已经确保在委托调用完成后释放相机拍摄的图像。例如,让我们假设这就是开发人员的样子(ARC 之前,向您展示您甚至不需要 ARC 来拥有它)。
- (IBAction)userDidPressAccept:(id)sender
{
// Obtain image from wherever it came from, this image will start with
// Retain Count: 1
UIImage *image = [[UIImage alloc] init];
// Build an NSDictionary, and add the image in
// Image Retain Count: 2
NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:image, UIImagePickerControllerOriginalImage, ..., nil];
// Now the dictionary has ownership of the image, we can safely release it
// Image Retain Count: 1
[image release];
if ([self.delegate respondsToSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:)])
{
// Guy sees what he does with his image
// Image Retain Count: X (Depends on the user)
[self.delegate imagePickerController:self didFinishPickingMediaWithInfo:image];
}
// Ok, we're done. Dictionary gets released, and it can no longer own the image
// Image Retain Count: X-1 (Depends on the user)
[userInfo release];
}
在示例中,如果用户没有retain
图像(或者甚至没有实现该方法),则 X 将为 1,当它到达 finalrelease
时,图像将永远消失。如果用户确实保留了图像,那么图像将继续存在,但支持它的字典可能会被dealloc
淘汰。
这就是引用计数带来的“所有权”的基本概念,它就像一个玻璃球,需要用手传过去,如果球在它下面没有手,它就会掉下来摔碎。
ARC 通过自己来掩盖了这一切,但基本概念仍然存在,所有权转移到委托的实现,如果没有委托声明它,它将被删除。