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?