如何自动释放 CGImageRef?
我有一个返回 CGImageRef 对象的方法。它包含这个调用:
CGImageRef posterFrame = [avAssetImage copyCGImageAtTime:time actualTime:nil error:&error];
...
return posterFrame;
据我了解,这种情况需要自动释放。但我不知道该怎么做。我尝试过使用 CFCollectable()
,但它不起作用。有什么想法吗?
I have a method that returns a CGImageRef
object. It contains this call:
CGImageRef posterFrame = [avAssetImage copyCGImageAtTime:time actualTime:nil error:&error];
...
return posterFrame;
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用垃圾回收,请使用
CFMakeCollectable(posterFrame)
。如果您使用传统的内存管理,则非常简单:将
CFTypeRef
(在本例中为CGImageRef
)转换为 Objective-C 对象指针,将其发送给-autorelease
消息,然后将结果转换回CGImageRef
。此模式适用于(几乎)任何与CFRetain()
和CFRelease()
兼容的类型。If you're using garbage collection, use
CFMakeCollectable(posterFrame)
. If you're using traditional memory management, it's very straightforward:You cast the
CFTypeRef
(in this case, aCGImageRef
) to an Objective-C object pointer, send it the-autorelease
message, and then cast the result back toCGImageRef
. This pattern works for (almost) any type that's compatible withCFRetain()
andCFRelease()
.Core Graphics(和 Core Foundation)没有 自动释放池,因此如果您想返回您的
CGImageRef
您需要将您的函数命名为包含Create
(feMyCreatePosterImage()
) 或返回UIImage
对象:Core Graphics (and Core Foundation) doesn't have an autorelease pool, so if you want to return your
CGImageRef
you need to either name your function containingCreate
(f.e.MyCreatePosterImage()
) or returnUIImage
object instead: