首页 > 使用 CGImageRef 偶尔出现内存泄露

使用 CGImageRef 偶尔出现内存泄露

自己做的自定义相册,在选择照片的时候有如下的一段代码:

    CGImageRef imageRef = [[asset defaultRepresentation] fullResolutionImage];
    if (imageRef == NULL) {
        imageRef = [asset aspectRatioThumbnail];
    }
    NSDictionary *info = @{UIImagePickerControllerOriginalImage:[UIImage imageWithCGImage:imageRef scale:1 orientation:(UIImageOrientation)[[asset defaultRepresentation] orientation]]};
    [self.delegate imagePickerController:nil
           didFinishPickingMediaWithInfo:info];

运行的时候我用 Instruments 进行检查,结果发现在选择照片的时候偶尔会会出现内存泄露,定位以后发现是这样的:

这意思是不是就是说iamgeRef出现了内存泄露?可是我在方法的最后加上了CGImageRelease(imageRef);则在第二次调用该方法的时候一定会闪退,这是怎么回事呢?(mrc 的基本原理我知道,但是我的小小的知识放在这里解决不了问题了 QAQ)


CGImageRef 需要手动释放内存,必须调用CGImageRelease(imageRef)释放内存;
第二次调用崩溃肯定是内存已经释放后再次释放,当然崩溃.

【热门文章】
【热门文章】