Skip to content

Commit

Permalink
fix: handle write errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lodev09 committed Mar 17, 2024
1 parent 6056285 commit 098db6f
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions ios/ExifyUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func getExifTags(from metadata: NSDictionary) -> [String: Any] {
func readExifTags(from url: URL?, completionHandler: ([String: Any]?) -> Void) -> Void {
guard let url, let sourceImage = CGImageSourceCreateWithURL(url as CFURL, nil),
let metadataDict = CGImageSourceCopyPropertiesAtIndex(sourceImage, 0, nil) else {
completionHandler(nil)
return
}

Expand All @@ -73,6 +74,7 @@ func readExifTags(from url: URL?, completionHandler: ([String: Any]?) -> Void) -

func updateMetadata(url: URL, with tags: [String: Any], completionHanlder: (NSDictionary?, Data?) -> Void) -> Void {
guard let cgImageSource = CGImageSourceCreateWithURL(url as CFURL, nil) else {
completionHanlder(nil, nil)
return
}

Expand All @@ -81,7 +83,7 @@ func updateMetadata(url: URL, with tags: [String: Any], completionHanlder: (NSDi

// Append additional Exif data
let exifDict = metadata[kCGImagePropertyExifDictionary as String] as? NSMutableDictionary
exifDict!.addEntries(from: tags)
exifDict?.addEntries(from: tags)

// Handle GPS Tags
var gpsDict = [String: Any]()
Expand Down Expand Up @@ -122,14 +124,15 @@ func updateMetadata(url: URL, with tags: [String: Any], completionHanlder: (NSDi

let destinationData = NSMutableData()

guard let sourceType = CGImageSourceGetType(cgImageSource),
guard let uiImage = UIImage(contentsOfFile: url.path),
let sourceType = CGImageSourceGetType(cgImageSource),
let destination = CGImageDestinationCreateWithData(destinationData, sourceType, 1, nil) else {
completionHanlder(nil, nil)
return
}

CGImageDestinationAddImageFromSource(destination, cgImageSource, 0, metadata)
if CGImageDestinationFinalize(destination) {
completionHanlder(metadata, destinationData as Data)
}
CGImageDestinationAddImage(destination, uiImage.cgImage!, metadata)
CGImageDestinationFinalize(destination)

completionHanlder(metadata, destinationData as Data)
}

0 comments on commit 098db6f

Please sign in to comment.