Jean Lebrument
Jean Lebrument

Reputation: 5169

CGImageRelease in Swift

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 !

Upvotes: 23

Views: 9261

Answers (2)

Jirui
Jirui

Reputation: 189

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/

Upvotes: 1

Rob Napier
Rob Napier

Reputation: 299595

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.

Upvotes: 56

Related Questions