UIImagePickerController,当didFinishPickingMediaWithInfo未实现时

时间:2013-07-19 07:09:52

标签: iphone ios objective-c uiimagepickercontroller

一个快速的概念性问题,

如果我正在使用UIImagePickerController,并且我没有实现didFinishPickingMediaWithInfo,或者在“拍摄”图片后尝试处理返回的UIImage,那么在委托调用期间返回的UIImage数据会发生什么变化呢刚刚由系统发布?

经过测试,它似乎没有泄漏或被添加到标准照片库中。

谢谢,

1 个答案:

答案 0 :(得分:1)

我不明白为什么它会在第一时间泄漏。

您必须认为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,当它到达最终release时,图像将永远消失。如果用户确实保留了图像,那么图像将继续存在,但支持它的字典可能会被dealloc - 使用。

这是参考计数带来的“所有权”的基本概念,它就像一个需要手工传递的玻璃球,如果球没有手,它就会掉落并破碎。

ARC有点通过自己做这件事掩盖所有这些,但基本概念仍然存在,所有权转移到委托的实现,如果没有声明它的委托,它将被删除。

相关问题