From 18f9d8158d25ade7a4459b9ee684df598db633cc Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Wed, 18 Mar 2020 13:02:27 +0100 Subject: [PATCH] fix(ios): Make Camera.getPhoto return exif from gallery photos (#2595) --- ios/Capacitor/Capacitor/Plugins/Camera.swift | 52 +++++++++++++++++--- 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/ios/Capacitor/Capacitor/Plugins/Camera.swift b/ios/Capacitor/Capacitor/Plugins/Camera.swift index 0082b1edc..5c6e7ae2f 100644 --- a/ios/Capacitor/Capacitor/Plugins/Camera.swift +++ b/ios/Capacitor/Capacitor/Plugins/Camera.swift @@ -158,15 +158,25 @@ public class CAPCameraPlugin : CAPPlugin, UIImagePickerControllerDelegate, UINav func showPhotos(_ call: CAPPluginCall) { let photoAuthorizationStatus = PHPhotoLibrary.authorizationStatus() - if photoAuthorizationStatus == .restricted || photoAuthorizationStatus == .denied { - call.error("User denied access to photos") - return + if (photoAuthorizationStatus != PHAuthorizationStatus.authorized) { + PHPhotoLibrary.requestAuthorization({ (status) in + if (status != PHAuthorizationStatus.authorized) { + call.error("User denied access to photos") + return + } else { + DispatchQueue.main.async { + self.presentPhotos() + } + } + }) + } else { + presentPhotos() } + } + private func presentPhotos() { self.configurePicker() - self.imagePicker!.sourceType = .photoLibrary - self.bridge.viewController.present(self.imagePicker!, animated: true, completion: nil) } @@ -197,7 +207,13 @@ public class CAPCameraPlugin : CAPPlugin, UIImagePickerControllerDelegate, UINav image = originalImage } - let imageMetadata = info[UIImagePickerController.InfoKey.mediaMetadata] as? [AnyHashable: Any] + var imageMetadata: [AnyHashable: Any] = [:] + if let photoMetadata = info[UIImagePickerController.InfoKey.mediaMetadata] as? [AnyHashable: Any] { + imageMetadata = photoMetadata + } + if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset { + imageMetadata = getImageMeta(asset: asset)! + } if settings.shouldResize { guard let convertedImage = resizeImage(image!) else { @@ -257,6 +273,30 @@ public class CAPCameraPlugin : CAPPlugin, UIImagePickerControllerDelegate, UINav picker.dismiss(animated: true, completion: nil) } + func metadataFromImageData(data: NSData)-> [String: Any]? { + let options = [kCGImageSourceShouldCache as String: kCFBooleanFalse] + if let imgSrc = CGImageSourceCreateWithData(data, options as CFDictionary) { + let metadata = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options as CFDictionary) as! [String: Any] + return metadata + } + return nil + } + + func getImageMeta(asset: PHAsset) -> [String:Any]?{ + let options = PHImageRequestOptions() + options.isSynchronous = true + options.resizeMode = .none + options.isNetworkAccessAllowed = false + options.version = .current + var meta:[String:Any]? = nil + _ = PHCachingImageManager().requestImageData(for: asset, options: options) { (imageData, dataUTI, orientation, info) in + if let data = imageData { + meta = self.metadataFromImageData(data: data as NSData) + } + } + return meta + } + func resizeImage(_ image: UIImage) -> UIImage? { let isAspectScale = settings.width > 0 && settings.height == 0 || settings.height > 0 && settings.width == 0 let aspect = Float(image.size.width / image.size.height);