CGImageRef cg = [[UIImage imageNamed: Path] CGImage]; Requires a CGImageRelease(cg)'?

核能气质少年 提交于 2019-12-11 02:29:50

问题


I am trying to read the ARGB pixels from a an image asset in iOS. For that, I need a CGImageRef I can use to get its CGDataProvider. My question is, if I create a CGImageRef using:

CGImageRef cg = [[UIImage imageNamed: Path] CGImage];

Will I eventually need to call CGImageRelease(cg)? If I don't call CGImageRelease, will I have a memory leak?

Another issue I am having is that reading the same file for a second time returns an empty image, which I suspect might be because I didn't call CGImageRelease the first time.


回答1:


You have to call CGImageRelease only when you use CGImageCreate, Copy or Retain(Or any such related method with create, copy, retain. For eg:- CGBitmapContextCreateImage). In this case, you dont have to do that since you are not doing any create, copy or retain.




回答2:


no, the CGImage will be bound to the UIImage's lifetime

following cocoa's 'create rule': you only need to release what you own (objects from methods named new*, copy*, retain return an ownership)

In a addition any object from CF, follows the CF create rules (only objects from methods named Create*, Copy*, Retain)



来源:https://stackoverflow.com/questions/13921779/cgimageref-cg-uiimage-imagenamed-path-cgimage-requires-a-cgimagerelease

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!