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)
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, orCFAutoreleasefunctions yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With