2015-01-10 7 views
11

CGBitmapContextCreate를 사용하여 컨텍스트를 만들었습니다. CGContextRelease를 사용하여 릴리즈해야합니까? Objective-C에서는 대답이 '예'라는 것을 알고 있지만 Swift에서는 어때요?Swift에서 CGContextRef를 출시해야합니까?

감사합니다.

+0

예, 당신은 목표 C – sapi

+0

같이 스위프트에서 동일한 규칙을 따라야합니다 :

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

반환 값은 당신이 좋아하는 자신을 볼 것 해제해야합니다 감사! 그렇다면 CGImageRef도 릴리스해야한다는 의미입니까? 다음 두 줄의 코드가 있습니다 : var imageRef = CGBitmapContextCreateImage (context); var image = UIImage (CGImage : imageRef) imageRef도 출시해야합니까? – user2732722

+0

@sapi 그들은 같은 규칙을 따르고 있습니까? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html – rakeshbs

답변

18

명시 적으로 지정되지 않으면 CFTypes가 자동으로 관리됩니다.
설명서에 따르면. 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.

단독 타입 타입 시그니처

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

CGBitmapContextCreate있을 것이다 타입 시그니처를 따라서는 빠른 의해 자동 관리

func CGBitmapContextCreate(...) -> CGContext! 

있다.

7

아니요, 아니요, CGContextRelease으로 전화하지 않아도됩니다.

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

CGContext 인스턴스가 자동 스위프트에 관리되는 메모리입니다 : 사실,하려고하면이 오류를 제공합니다. 당신은 함수 서명에서 알 수 있습니다 :

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