管理CGImageRef内存的规则?

Cha*_*ice 2 cocoa-touch objective-c ios automatic-ref-counting

CGImageRef使用ARC 管理内存的规则有哪些?也就是说,有人可以帮我找到正确的文档吗?

我从照片库中获取图像并创建一个UIImage显示:

CGImageRef newImage = [assetRep fullResolutionImage];
...
UIImage *cloudImage = [UIImage imageWithCGImage:newImage scale:scale orientation:orientation];
Run Code Online (Sandbox Code Playgroud)

我需要做CGImageRelease(newImage)什么?

我得到内存警告,但它不似乎是我还没有发布对象的逐渐积累,我没有看到有任何仪器泄漏.我很困惑.

NSG*_*God 7

不,你不需要调用CGImageRelease()CGImageRef由归国ALAssetRepresentation的便利方法,如fullResolutionImagefullScreenImage.不幸的是,目前这些方法的文档和头文件并没有说清楚.

如果您CGImageRef使用其中一个CGImageCreate*()函数创建自己,那么您拥有它并负责释放该图像引用CGImageRelease().相比之下,由于您不拥有这些方法返回的图像引用,因此CGImageRef返回fullResolutionImagefullScreenImage显示为"自动释放" 的s .例如,假设你在代码中尝试这样的事情:

CGImageRef newImage = [assetRep fullResolutionImage];
...
UIImage *cloudImage = [UIImage imageWithCGImage:newImage
                        scale:scale orientation:orientation];
CGImageRelease(newImage);
Run Code Online (Sandbox Code Playgroud)

如果您运行静态分析器,它将为该CGImageRelease(newImage);行发出以下警告:

调用者此时不拥有的对象的引用计数的不正确的减少

请注意,无论您的项目是设置为使用手动参考计数还是ARC,都会收到此警告.

相比之下,例如,CGImage方法的文档NSBitmapImageRep使得CGImageRef返回的自动释放更加清晰:

CGImage

从接收器的当前位图数据返回Core Graphics图像对象.

- (CGImageRef)CGImage

回报价值

CGImageRef根据接收者的当前位图数据返回自动释放的opaque类型.

  • 您只需要记住Core Foundation内存管理规则.如果函数名称中包含"Create",则管理其内存; 如果没有,那么你没有. (2认同)