2015-01-10 17 views
11

私はCGBitmapContextCreateを使ってコンテキストを作成しました。 CGContextReleaseを使ってリリースする必要がありますか? Objective-Cでは答えが「はい」であることはわかっていますが、Swiftではどうですか?SwiftでCGContextRefをリリースする必要がありますか?

ありがとうございます!

+0

はい、あなたはObjective Cの内とスウィフトで同じルールに従う必要があり – sapi

+0

ありがとう!それで、CGImageRefもリリースするべきですか?私は次の2行のコードを持っています: 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は、Unmanagedとして明示的に指定されていない限り、自動的に管理されます。
ドキュメントによると。 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タイプシグネチャしたがってそのSWIFTによって自動的に管理

func CGBitmapContextCreate(...) -> CGContext! 

を有している必要があります。

7

いいえ、CGContextReleaseに電話する必要はありません。実際には、しようとすると、あなたにこのエラーを与える:

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

CGContextインスタンスは自動的スウィフトに管理するメモリです。あなたは、関数のシグネチャから伝えることができます:

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

あなた自身を解放する必要があります戻り値は次のようになります。

func CGBitmapContextCreate(/* several parameters */) -> Unmanaged<CGContext>! 
関連する問題