22

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 !

Shruti Thombre
  • 989
  • 4
  • 11
  • 27
Jean Lebrument
  • 5,079
  • 8
  • 33
  • 67

2 Answers2

55

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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I doubt it. ARC introduces a number of restrictions that are pretty easy to avoid in ObjC, but extremely common in C. You can't put an ARC-managed element in a `struct` for instance. It is tricky to pass an ARC-managed element as a `void*`. Apple's focus is on Swift, and I doubt they would spend a lot of time enhancing C. They would tend, IMO, to spend that time improving Swift as a replacement for whatever you were doing in C. – Rob Napier Mar 18 '15 at 14:55
  • If _CGImageRelease_ is not needed anymore why memory usage is increasing everytime we use _context.createCGImage_ – Hope Dec 21 '15 at 07:57
  • @the It depends on the specifics of your case. Open a question that with code that reproduces your symptom and a description of exactly what you're seeing. – Rob Napier Dec 21 '15 at 13:15
1

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/

Jirui
  • 139
  • 1
  • 6