Skip to content

Commit

Permalink
Merge pull request #248 from murat-mehmet/patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
numandev1 authored Nov 17, 2023
2 parents af18f76 + 5428396 commit 958586f
Showing 1 changed file with 15 additions and 6 deletions.
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

0 comments on commit 958586f

Please sign in to comment.