Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGImageRelease in Swift

Tags:

ios

swift

cgimage

In Objc I use CGImageRelease method after the treatment of an image. But in Swift this method is not available.

On the Apple documentation after Retaining and Releasing Images there is a 2 Objective-C symbols hidden

My question is, why is there no more CGImageRelease in Swift ? And have we to call another method to replace it ?

Thanks !

like image 717
Jean Lebrument Avatar asked Sep 11 '14 14:09

Jean Lebrument


2 Answers

CGImage is now managed by ARC. CGImageRelease() is no longer required on it. You can know this by looking in CGImage.h and noting that it includes the macro CF_IMPLICIT_BRIDGING_ENABLED. This indicates that Apple has audited this file to make sure it conforms to memory-management naming conventions so ARC can memory manage objects returned from functions in this file.


EDIT: I was reading over this and realized I was misleading. I don't mean to say that CGImageRelease isn't needed in ObjC (which is pretty much what I implied here…) I just mean that because of the auditing, Swift is able to handle it. In ObjC code, you still need to release these objects.

like image 119
Rob Napier Avatar answered Nov 01 '22 21:11

Rob Napier


There is no more CGImageRelease in Swift, and there is no another method to replace it.

  • Swift uses ARC exclusively, so there’s no room for a call to CFRelease or __bridge_retained.
  • For types returned from C functions, we can call takeRetainedValue() / takeUnretainedValue() to get a Swift-managed value.
  • With the macro CF_IMPLICIT_BRIDGING_ENABLED, that turn on the Clang arc_cf_code_audited, Swift can handle the memory management for return value

For detail: https://nshipster.com/unmanaged/

like image 1
Jirui Avatar answered Nov 01 '22 20:11

Jirui