Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use new image metadata as destination when copying exif data #248

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions ios/Image/ImageCompressor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,33 @@ class ImageCompressor {

let url = URL(fileURLWithPath: filePath)
let source = CGImageSourceCreateWithURL(url as CFURL, nil)!
var metadata = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any]
let metadata = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any]

let dataProvider = CGDataProvider(data: data as CFData)
let dataImageSource = CGImageSourceCreateWithDataProvider(dataProvider!, nil)!
let dataMetadata = CGImageSourceCopyPropertiesAtIndex(dataImageSource, 0, nil) as? [CFString: Any]
var dataMetadata = CGImageSourceCopyPropertiesAtIndex(dataImageSource, 0, nil) as? [CFString: Any]

// Copy all keys from source metadata to destination metadata if they don't exist
for (key, value) in dataMetadata ?? [:] {
if metadata?[key] == nil {
metadata?[key] = value
for (key, value) in metadata ?? [:] {
if dataMetadata?[key] == nil {
dataMetadata?[key] = value
} else {
if let metadataObj = dataMetadata?[key] as? NSMutableDictionary,
let valueObj = value as? NSDictionary {
for (key, value) in valueObj {
if metadataObj[key] == nil {
metadataObj[key] = value
}
}
}
}
}

let outputFormat = isPNG(data) ? kUTTypePNG : kUTTypeJPEG

let destinationData = NSMutableData()
let destination = CGImageDestinationCreateWithData(destinationData, outputFormat, 1, nil)!
CGImageDestinationAddImage(destination, image.cgImage!, metadata as CFDictionary?)
CGImageDestinationAddImage(destination, image.cgImage!, dataMetadata as CFDictionary?)
CGImageDestinationFinalize(destination)
return destinationData as Data
}
Expand Down
Loading