【问题标题】:CGImageDestinationCreateWithData constants in iOSiOS 中的 CGImageDestinationCreateWithData 常量
【发布时间】: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


    【解决方案1】:

    来自《iOS 技术概览》中的Mobile Core Services Framework

    移动核心服务框架 (MobileCoreServices.framework) 定义了统一类型标识符 (UTI) 中使用的低级类型。

    有关此框架定义的类型的更多信息,请参阅Uniform Type Identifiers Reference

    所以

    import MobileCoreServices
    

    制作

    public let kUTTypePNG: CFString
    

    以及其他可用于您的代码的 UTI 常量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多