CGImageDestinationCreateWithData constants in iOS
Asked Answered
L

1

1

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?

Leech answered 12/8, 2016 at 11:5 Comment(0)
W
7

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.

Witness answered 12/8, 2016 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.