【问题标题】:UIImagePickerController, when didFinishPickingMediaWithInfo not implementedUIImagePickerController,当 didFinishPickingMediaWithInfo 未实现时
【发布时间】:2013-07-18 09:11:44
【问题描述】:

一个快速的概念问题,

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

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

谢谢,

【问题讨论】:

    标签: iphone ios objective-c uiimagepickercontroller


    【解决方案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-ated。

    这是引用计数带来的“所有权”的基本概念,它就像一个玻璃球,需要用手递过去,如果球下面没有手,它就会掉下来碎掉。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多