iphone - UIImagePickerController, didFinishPickingMediaWithInfo 没有实现的时候

标签 iphone ios objective-c uiimagepickercontroller

一个快速的概念性问题,

如果我正在使用 UIImagePickerController,并且我没有实现 didFinishPickingMediaWithInfo,或者在“拍摄”图片后尝试处理返回的 UIImage,那么在委托(delegate)调用期间本应返回的 UIImage 数据会发生什么情况,是吗?刚被系统释放?

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

谢谢,

最佳答案

一开始我不明白为什么它会泄漏。

您一定认为 Apple 开发人员编写的代码已经确保在委托(delegate)调用完成后释放相机拍摄的图像。例如,让我们假装这就是开发人员的样子(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 有点通过自己来掩盖所有这些,但基本概念仍然存在,所有权转移到委托(delegate)的实现,如果没有委托(delegate)声明它,它将被删除。

关于iphone - UIImagePickerController, didFinishPickingMediaWithInfo 没有实现的时候,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17740058/

相关文章:

ios - 当键盘可见时 UIScrollView 滚动到按钮

ios - iOS是否延迟peripheralManager :didReceiveWriteRequests: and peripheralManager:didReceiveReadRequest:?

ios - 如何从应用程序扩展更新的 sqlite 文件中重新加载主应用程序中的 CoreData 数据?

即使应用程序关闭,iOS 也会在特定时间安排任务?

objective-c - Objective-C 和 iOS : running a timer? NSTimer/线程/NSDate/等

iphone - 重命名表格 View 中的标准 "edit"按钮

ios - 使用界面生成器将选项卡栏添加到 NavigationController,一点也不直观

iphone - 如何加强 Java ME 生态系统

ios - 尝试在 flex ios 中写入只读数据库

objective-c - NSWindow 不会在 OSStatus 事件处理程序方法中显示