2015-01-10 5 views
11

मैंने CGBitmapContextCreate का उपयोग कर एक संदर्भ बनाया है। क्या मुझे CGContextRelease का उपयोग करके इसे रिलीज़ करने की ज़रूरत है? मुझे पता है कि जवाब उद्देश्य-सी में है, लेकिन स्विफ्ट में कैसे?क्या आपको स्विफ्ट में CGContextRef जारी करने की आवश्यकता है?

धन्यवाद!

+0

हाँ, आप उद्देश्य सी – sapi

+0

में के रूप में स्विफ्ट में एक ही नियमों का पालन करने की जरूरत है:

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

एक वापसी मान आप की तरह अपने आप को लगेगा जारी करने के लिए की आवश्यकता होगी धन्यवाद! तो क्या इसका मतलब यह है कि मुझे CGImageRef भी जारी करना चाहिए? मेरे पास कोड की निम्नलिखित दो पंक्तियां हैं: var imageRef = CGBitmapContextCreateImage (संदर्भ); 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>! 
संबंधित मुद्दे

 संबंधित मुद्दे