【发布时间】:2016-12-19 09:07:56
【问题描述】:
我有以下代码将CGImage 转换为NSData:
import Foundation
import CoreGraphics
import ImageIO
// ... snip ...
let data = NSMutableData()
if let dest = CGImageDestinationCreateWithData(data, kUTTypePNG, 1, nil), let image = self.backgroundImage {
CGImageDestinationAddImage(dest, image, nil)
if CGImageDestinationFinalize(dest) {
return data as Data
}
}
return nil
代码在 Mac-OS 中编译良好,但 kUTTypePNG 在 iOS 中未定义。常量的实际值为"public.png",显然,用该值替换常量可以让iOS 编译代码正常。
但避免使用魔法字符串/数字是我们首先使用常量的原因 - 在 Swift-iOS 中是否有替代常量?
【问题讨论】:
标签: ios swift core-graphics core-image