gpt4 book ai didi

swift - 你需要在 Swift 中释放 CGContextRef 吗?

转载 作者:IT王子 更新时间:2023-10-29 05:22:27 25 4
gpt4 key购买 nike

我使用 CGBitmapContextCreate 创建了一个上下文。我需要使用 CGContextRelease 来发布它吗?我知道在 Objective-C 中答案是肯定的,但在 Swift 中呢?

谢谢!

最佳答案

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!

因此它由 swift 自动管理。

关于swift - 你需要在 Swift 中释放 CGContextRef 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27872822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com