Last active
May 3, 2019 17:39
-
-
Save rayfix/188b2dc70341c00f7d800e2528a54977 to your computer and use it in GitHub Desktop.
HEIC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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