CGImageDestinationCreateWithData constants in iOS

谁都会走 提交于 2019-12-12 05:17:54

问题


I have the following code which turns a CGImage into 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

The code compiles fine in Mac-OS, but kUTTypePNG is undefined in iOS. The actual value of the constant is "public.png", and obviously, replacing the constant with that value allows iOS to compile the code fine.

But avoiding magic strings/numbers is the reason we use constants in the first place - is there an alternative constant in Swift-iOS?


回答1:


From Mobile Core Services Framework in the "iOS Technology Overview":

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.

So

import MobileCoreServices

makes

public let kUTTypePNG: CFString

and other UTI constants available to your code.



来源:https://stackoverflow.com/questions/38916484/cgimagedestinationcreatewithdata-constants-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!