开发者

How to Autorelease a CGImageRef?

开发者 https://www.devze.com 2023-03-27 21:49 出处:网络
I have a method that returns a CGImageRef object. It contains this call: CGImageRef posterFrame = [avAssetImage copyCGImageAtTime:time actualTime:nil error:&error];

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 d开发者_Python百科oesn't work. Any ideas?


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


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 containing Create(f.e. MyCreatePosterImage()) or return UIImage object instead:

CGImageRef posterFrame = [avAssetImage copyCGImageAtTime:time actualTime:nil error:&error];
UIImage *result = [UIImage imageWithCGImage:posterFrame];
CGImageRelease(posterFrame);
return result;
0

精彩评论

暂无评论...
验证码 换一张
取 消