2015-01-10 5 views
11

Ho creato un contesto mediante CGBitmapContextCreate. Devo rilasciarlo usando CGContextRelease? So che la risposta è sì in Objective-C, ma che ne dici di Swift?Hai bisogno di rilasciare CGContextRef in Swift?

Grazie!

+0

Sì, è necessario seguire le stesse regole a Swift come in Objective C – sapi

+0

Grazie! Quindi vuol dire che dovrei anche rilasciare il CGImageRef? Ho le seguenti due righe di codice: var imageRef = CGBitmapContextCreateImage (contesto); immagine var = UIImage (CGImage: imageRef) Dovrei rilasciare anche imageRef? – user2732722

+0

@sapi Sei sicuro che seguono le stesse regole? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html – rakeshbs

risposta

18

CFTypes sono gestiti automaticamente se non espressamente specificato come non gestito.
Secondo la documentazione. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

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. If you return Core Foundation objects from your own C functions and Objective-C methods, annotate them with either CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED. The compiler automatically inserts memory management calls when it compiles Swift code that invokes these APIs. If you use only annotated APIs that do not indirectly return Core Foundation objects, you can skip the rest of this section. Otherwise, continue on to learn about working with unmanaged Core Foundation objects.

When Swift imports APIs that have not been annotated, the compiler cannot automatically memory manage the returned Core Foundation objects. Swift wraps these returned Core Foundation objects in an Unmanaged structure.

tipi non gestiti avranno la firma tipo

func StringByAddingTwoStrings(CFString!, CFString!) -> Unmanaged<CFString>! 

CGBitmapContextCreate ha la firma di tipo

func CGBitmapContextCreate(...) -> CGContext! 

qui il suo gestita automaticamente dal rapido.

7

No, non è necessario chiamare CGContextRelease. In realtà, cercando di ti dà questo errore:

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

CGContext casi sono automaticamente memoria gestita a Swift. Si può dire dalla firma della funzione:

func CGBitmapContextCreate(/* several parameters */) -> CGContext! 

Un valore di ritorno si avrebbe bisogno di liberare te stesso sarà simile:

func CGBitmapContextCreate(/* several parameters */) -> Unmanaged<CGContext>!