ios - iOS 中的 CGImageDestinationCreateWithData 常量

标签 ios swift core-graphics core-image

我有以下代码将 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 中是否有替代常量?

最佳答案

来自 Mobile Core Services Framework在“iOS 技术概述”中:

The Mobile Core Services framework (MobileCoreServices.framework) defines the low-level types used in uniform type identifiers (UTIs).

For more information about the types defined by this framework, see Uniform Type Identifiers Reference.

所以

import MobileCoreServices

制作

public let kUTTypePNG: CFString

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

关于ios - iOS 中的 CGImageDestinationCreateWithData 常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38916484/

相关文章:

ios - 双击 Collection View 单元格时如何使图像可见?

java - Android 和 iOS 中的文件安全

swift - 如何在 Swift 3/iOS 10 中创建一个大小合适的 WKWebView

swift - 如何在按下位于自定义 UIView 内且带有 XCode Storyboard的 UIButton 时创建 Segue

iphone - 根据两个图像之间的差异创建蒙版 (iPhone)

ios - 这个保留周期的原因是什么?有没有办法强制释放内存?

ios - 使用隐藏的 SCNNode 捕获 SCNView 的图像/屏幕截图 : drawViewHierarchyInRect afterScreenUpdates not working

ios - AWS SNS 错误参数无效 : Token Reason: Endpoint xxx already exists with the same Token, 但属性不同

objective-c - 音频路由不改变时的耳机插拔事件 - iOS

Swift map 编译错误