mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 04:45:20 +00:00
Strip metadata in mac image encoding
This commit is contained in:
@@ -3,6 +3,8 @@ import Foundation
|
|||||||
import UIKit
|
import UIKit
|
||||||
#else
|
#else
|
||||||
import AppKit
|
import AppKit
|
||||||
|
import ImageIO
|
||||||
|
import UniformTypeIdentifiers
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum ImageUtilsError: Error {
|
enum ImageUtilsError: Error {
|
||||||
@@ -52,11 +54,24 @@ enum ImageUtils {
|
|||||||
let scaled = scaledImage(image, maxDimension: maxDimension)
|
let scaled = scaledImage(image, maxDimension: maxDimension)
|
||||||
guard let tiffData = scaled.tiffRepresentation,
|
guard let tiffData = scaled.tiffRepresentation,
|
||||||
let bitmap = NSBitmapImageRep(data: tiffData),
|
let bitmap = NSBitmapImageRep(data: tiffData),
|
||||||
let jpegData = bitmap.representation(using: .jpeg, properties: [.compressionFactor: compressionQuality]) else {
|
let cgImage = bitmap.cgImage else {
|
||||||
throw ImageUtilsError.encodingFailed
|
throw ImageUtilsError.encodingFailed
|
||||||
}
|
}
|
||||||
let outputURL = try makeOutputURL()
|
let outputURL = try makeOutputURL()
|
||||||
try jpegData.write(to: outputURL, options: .atomic)
|
guard let destination = CGImageDestinationCreateWithURL(outputURL as CFURL, UTType.jpeg.identifier as CFString, 1, nil) else {
|
||||||
|
throw ImageUtilsError.encodingFailed
|
||||||
|
}
|
||||||
|
let options: [CFString: Any] = [
|
||||||
|
kCGImageDestinationLossyCompressionQuality: compressionQuality,
|
||||||
|
kCGImagePropertyExifDictionary: [:],
|
||||||
|
kCGImagePropertyTIFFDictionary: [:],
|
||||||
|
kCGImagePropertyIPTCDictionary: [:],
|
||||||
|
kCGImagePropertyOrientation: 1
|
||||||
|
]
|
||||||
|
CGImageDestinationAddImage(destination, cgImage, options as CFDictionary)
|
||||||
|
guard CGImageDestinationFinalize(destination) else {
|
||||||
|
throw ImageUtilsError.encodingFailed
|
||||||
|
}
|
||||||
return outputURL
|
return outputURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user