我有一个返回 CGImageRef
对象的方法。它包含这个调用:
CGImageRef posterFrame = [avAssetImage copyCGImageAtTime:time actualTime:nil error:&error];
...
return posterFrame;
据我了解,这种情况需要自动释放。但我不知道该怎么做。我试过使用 CFCollectable()
,但它不起作用。有什么想法吗?
最佳答案
如果您正在使用垃圾回收,请使用 CFMakeCollectable(posterFrame)
。如果您使用的是传统内存管理,则非常简单:
return (CGImageRef)[(id)posterFrame autorelease];
您将 CFTypeRef
(在本例中为 CGImageRef
)转换为 Objective-C 对象指针,向其发送 -autorelease
消息,然后将结果转换回 CGImageRef
。此模式适用于(几乎)任何与 CFRetain()
和 CFRelease()
兼容的类型。
关于objective-c - 如何自动释放 CGImageRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061149/