本文介绍了发布CGImage(CGImageRef)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇我是否正在使用CGImageRef跟踪正确的内存管理,我正在通过几种方法(因为它是CG对象,我认为它不支持自动释放)。使用非NSObjects进行内存管理以及与其他NSObject的交互对我来说仍然有点新鲜。

I'm curious if I'm following proper memory management with CGImageRef, which I'm passing through several methods (since it's CG object, I assume it doesn't support autorelease). Memory management with non-NSObjects and interaction with other NSObjects is still somewhat new to me.

这里我正在做的事情:

我正在使用CGBitmapContextCreateImage(保留计数1)在我的图像缓存管理器中创建CGImageRef,并将其添加到NSMutableDictionary(保留计数2)。

I'm creating the CGImageRef inside my image cache manager with CGBitmapContextCreateImage (retain count 1), and adding it to the NSMutableDictionary (retain count 2).

当CALayers使用图像时,我分配了layer.contents(保留计数+1),并在删除图层之前使用layer.contents = nil(保留计数-1)清除内容。

When CALayers use the image, I assign with layer.contents (retain count +1), and clearing contents with layer.contents = nil (retain count -1) before removing the layer.

最后,在清除纹理时,我调用CGImageRefRelease和[NSMutableDictionary removeObject]使保留计数为0.。

Finally, when clearing the texture, I call CGImageRefRelease and [NSMutableDictionary removeObject] to have retain count as 0.

这是一种正确的方法吗?

Is this a proper way to do so?

推荐答案

您描述的步骤应该正常工作,不会泄漏或过度释放对象。

The steps you describe should work just fine and not leak or over-release the object.

我使用 CFRelease 而不是像 CGImageRelease 这样的特定类发布函数,但纯粹是作为一种风格问题。我只需要注意 NULL CGImageRelease 检查 NULL ,而 CFRelease 在传递 NULL 时会崩溃。使用 CGImageRelease 及其兄弟姐妹意味着你不必担心这个。

I use CFRelease instead of specific-class release functions like CGImageRelease, but purely as a matter of style. I just have to beware of NULL: CGImageRelease checks for NULL, whereas CFRelease will crash when passed NULL. Using CGImageRelease and its siblings means you don't have to worry about this.

我假设你的意思 removeObject:forKey:,而不是 removeObject (它不存在,无论如何也无法指定对象)。

I'm assuming you meant removeObject:forKey:, not removeObject (which doesn't exist and wouldn't have somewhere to specify an object anyway).

这篇关于发布CGImage(CGImageRef)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:32