2015-01-10 13 views
11

Utworzono kontekst przy użyciu CGBitmapContextCreate. Czy muszę zwolnić go za pomocą CGContextRelease? Wiem, że odpowiedź brzmi: tak w Objective-C, ale co powiesz na Swift?Czy chcesz zwolnić CGContextRef w Swift?

Dzięki!

+0

Tak, trzeba się do tych samych zasad w Swift jak w Objective C – sapi

+0

Dzięki! Czy to znaczy, że powinienem również wydać CGImageRef? Mam następujące dwa wiersze kodu: var imageRef = CGBitmapContextCreateImage (context); var image = UIImage (CGImage: imageRef) Czy powinienem również zwolnić imageRef? – user2732722

+0

@sapi Czy na pewno stosują te same zasady? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html – rakeshbs

Odpowiedz

18

Metody CFTypes są zarządzane automatycznie, chyba że wyraźnie określono, że są niezarządzane. Zgodnie z dokumentacją. 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.

Niezarządzane typy będą miały podpis typu

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

CGBitmapContextCreate ma podpisu TYP

func CGBitmapContextCreate(...) -> CGContext! 

Stąd jego zarządzany automatycznie przez SWIFT.

7

Nie, nie trzeba dzwonić pod numer CGContextRelease. W rzeczywistości, próbując daje ten błąd:

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

CGContext instancje są automatycznie pamięć zarządzana Swift. Można powiedzieć z podpisem funkcji:

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

Zwracana wartość trzeba by uwolnić się wyglądałby następująco:

func CGBitmapContextCreate(/* several parameters */) -> Unmanaged<CGContext>! 
Powiązane problemy