您需要在Swift中释放CGContextRef吗? [英] Do you need to release CGContextRef in Swift?

查看:419
本文介绍了您需要在Swift中释放CGContextRef吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用CGBitmapContextCreate创建了一个上下文.我需要使用CGContextRelease释放它吗?我知道在Objective-C中答案是肯定的,但是在Swift中呢?

I've created a context using CGBitmapContextCreate. Do I need to release it using CGContextRelease? I know the answer is yes in Objective-C, but how about in Swift?

谢谢!

推荐答案

除非明确指定为Unmanaged,否则将自动管理CFType.
根据文档. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

CFTypes are automatically managed unless explicitly specified as Unmanaged.
According to the documentation. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html

从注释的API返回的

Core Foundation对象是自动的 在Swift中管理的内存-您无需调用CFRetain, CFRelease或CFAutorelease自己起作用.如果您退还Core 来自您自己的C函数和Objective-C方法的基础对象, 用CF_RETURNS_RETAINED或注释它们 CF_RETURNS_NOT_RETAINED.编译器自动插入内存 在编译调用这些API的Swift代码时,管理调用. 如果仅使用不间接返回Core的带注释的API 基础对象,您可以跳过本节的其余部分.否则, 继续学习有关与非托管Core Foundation一起工作的信息 对象.

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.

当Swift导入未注释的API时,编译器 无法自动内存管理返回的Core Foundation 对象. Swift将这些返回的Core Foundation对象包装在一个 非托管结构.

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.

非托管类型将具有类型签名

Unmanaged types will have the type signature

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

CGBitmapContextCreate具有类型签名

func CGBitmapContextCreate(...) -> CGContext!

因此可以迅速对其进行自动管理.

Hence its managed automatically by swift.

这篇关于您需要在Swift中释放CGContextRef吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆