如何自动释放CGImageRef? [英] How to Autorelease a CGImageRef?
问题描述
我有一个返回CGImageRef
对象的方法.它包含此调用:
I have a method that returns a CGImageRef
object. It contains this call:
CGImageRef posterFrame = [avAssetImage copyCGImageAtTime:time actualTime:nil error:&error];
...
return posterFrame;
据我所知,这种情况需要自动释放.但是我不知道该怎么做.我试过使用CFCollectable()
,但是它不起作用.有什么想法吗?
This situation, as I understand it, calls for an autorelease. But I have no idea how to do it. I've tried using CFCollectable()
, but it doesn't work. Any ideas?
推荐答案
如果您正在使用垃圾回收,请使用CFMakeCollectable(posterFrame)
.如果您使用的是传统内存管理,则非常简单:
If you're using garbage collection, use CFMakeCollectable(posterFrame)
. If you're using traditional memory management, it's very straightforward:
return (CGImageRef)[(id)posterFrame autorelease];
将CFTypeRef
(在本例中为CGImageRef
)强制转换为Objective-C对象指针,将其发送给-autorelease
消息,然后将结果强制转换回CGImageRef
.此模式适用于(几乎)与CFRetain()
和CFRelease()
兼容的任何类型.
You cast the CFTypeRef
(in this case, a CGImageRef
) to an Objective-C object pointer, send it the -autorelease
message, and then cast the result back to CGImageRef
. This pattern works for (almost) any type that's compatible with CFRetain()
and CFRelease()
.
这篇关于如何自动释放CGImageRef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!