I have the following code that turns a CGImageinto NSData:
import Foundation
import CoreGraphics
import ImageIO
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 on Mac OS, but kUTTypePNGis undefined on iOS. The actual value of the constant "public.png", and, obviously, replacing the constant with this value allows iOS to compile the code in order.
But avoiding magic strings / numbers, we use constants in the first place - is there an alternative constant in Swift-iOS?
source
share