How to Autorelease a CGImageRef?

后端 未结 2 1932
青春惊慌失措
青春惊慌失措 2021-01-06 18:37

I have a method that returns a CGImageRef object. It contains this call:

CGImageRef posterFrame = [avAssetImage copyCGImageAtTime:time actualTim         


        
2条回答
  •  半阙折子戏
    2021-01-06 19:37

    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];
    

    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().

提交回复
热议问题