|
@@ -232,4 +232,35 @@ extension UIImage {
|
|
|
}
|
|
|
return image
|
|
|
}
|
|
|
+
|
|
|
+ var heic: Data? { heic() }
|
|
|
+ var cgImageOrientation: CGImagePropertyOrientation { .init(imageOrientation) }
|
|
|
+
|
|
|
+ func heic(compressionQuality: CGFloat = 1) -> Data? {
|
|
|
+ guard
|
|
|
+ let mutableData = CFDataCreateMutable(nil, 0),
|
|
|
+ let destination = CGImageDestinationCreateWithData(mutableData, "public.heic" as CFString, 1, nil),
|
|
|
+ let cgImage = cgImage
|
|
|
+ else { return nil }
|
|
|
+ CGImageDestinationAddImage(destination, cgImage, [kCGImageDestinationLossyCompressionQuality: compressionQuality, kCGImagePropertyOrientation: cgImageOrientation.rawValue] as CFDictionary)
|
|
|
+ guard CGImageDestinationFinalize(destination) else { return nil }
|
|
|
+ return mutableData as Data
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+extension CGImagePropertyOrientation {
|
|
|
+ init(_ uiOrientation: UIImage.Orientation) {
|
|
|
+ switch uiOrientation {
|
|
|
+ case .up: self = .up
|
|
|
+ case .upMirrored: self = .upMirrored
|
|
|
+ case .down: self = .down
|
|
|
+ case .downMirrored: self = .downMirrored
|
|
|
+ case .left: self = .left
|
|
|
+ case .leftMirrored: self = .leftMirrored
|
|
|
+ case .right: self = .right
|
|
|
+ case .rightMirrored: self = .rightMirrored
|
|
|
+ @unknown default:
|
|
|
+ fatalError()
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|