Skip to content

Instantly share code, notes, and snippets.

@rayfix
Last active May 3, 2019 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayfix/188b2dc70341c00f7d800e2528a54977 to your computer and use it in GitHub Desktop.
Save rayfix/188b2dc70341c00f7d800e2528a54977 to your computer and use it in GitHub Desktop.
HEIC
extension UIImage {
enum HEICError: Error {
case heicNotSupported
case cgImageMissing
case couldNotFinalize
}
static var isHEICSupported: Bool = {
let data = NSMutableData()
return CGImageDestinationCreateWithData(data, AVFileType.heic as CFString,
1, nil) != nil
}()
func heicData(compressionQuality: CGFloat) throws -> Data {
let data = NSMutableData()
guard let imageDestination =
CGImageDestinationCreateWithData(data, AVFileType.heic as CFString, 1, nil)
else {
throw HEICError.heicNotSupported
}
guard let cgImage = self.cgImage else {
throw HEICError.cgImageMissing
}
let options: NSDictionary = [kCGImageDestinationLossyCompressionQuality: compressionQuality]
CGImageDestinationAddImage(imageDestination, cgImage, options)
guard CGImageDestinationFinalize(imageDestination) else {
throw HEICError.couldNotFinalize
}
return data as Data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment