3

I have the following code in Swift:

 func cropToAlpha(image:CGImage) -> CGImage? {
    let pixelDataProvider:CGDataProvider? = CGImageGetDataProvider(image)
    if pixelDataProvider != nil {
        let pixelData:CFData? = CGDataProviderCopyData(pixelDataProvider!)

The documentation for CGImageGetDataProvider and the documentation for CGDataProviderCopyData both say

You are responsible for releasing this object

The Swift programming guide talks about deinitialization but that's not the same as releasing an object.

Some Objective-C answers (e.g. How do I release a CGImageRef in iOS) suggest calling CFRelease but if I try something like

CFRelease(pixelDataProvider)

then I get the compiler error

'CFRelease' is unavailable: Core Foundation objects are automatically memory managed

How do I honor the request in the documentation for CGImageGetDataProvider and CGDataProviderCopyData by releasing the resulting objects in Swift?

(N.B. Searching the web for 'Swift release' is tricky as the search results are awash with pages about releases of Swift)

Community
  • 1
  • 1
dumbledad
  • 16,305
  • 23
  • 120
  • 273

1 Answers1

10

You don't have to release it. The documentation is still targeted towards Objective-C. The compiler error message is correct.

From Using Swift with Cocoa and Objective-C:

Core Foundation objects returned from annotated APIs are automatically memory managed in Swift—you do not need to invoke the CFRetain, CFRelease, or CFAutorelease functions yourself.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256