Skip to content

Commit

Permalink
fix(ios): Make Camera.getPhoto return exif from gallery photos (#2595)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Mar 18, 2020
1 parent 0bfa0bf commit 18f9d81
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions ios/Capacitor/Capacitor/Plugins/Camera.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 18f9d81

Please sign in to comment.