Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HugoSay/e200ab5dad9c5e651bf79b85605b97af to your computer and use it in GitHub Desktop.
Save HugoSay/e200ab5dad9c5e651bf79b85605b97af to your computer and use it in GitHub Desktop.
get an UIImage and a file containing the exif of the selected image.
static public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) -> (image: UIImage, url: URL)? {
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return nil }
var url: URL?
if #available(iOS 11.0, *) {
url = info[.imageURL] as? URL
} else {
url = info[.referenceURL] as? URL
}
if picker.sourceType == UIImagePickerController.SourceType.photoLibrary {
var asset: PHAsset?
if #available(iOS 11.0, *) {
asset = info[.phAsset] as? PHAsset
} else {
if let url = url {
asset = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil).firstObject
}
}
if let asset = asset {
let imagePath: String = NSTemporaryDirectory() + "temp.jpg"
let imageUrl: URL = URL(fileURLWithPath: imagePath)
let options = PHImageRequestOptions()
options.isSynchronous = true
PHImageManager.default().requestImageData(for: asset, options: options) { (data, string, orientation, dict) in
try? data?.write(to: imageUrl)
}
return (image: image, url: imageUrl)
}
} else { // source is the camera.
if let metadata = info[UIImagePickerController.InfoKey.mediaMetadata] as? [String: Any],
let ciImage = image.cgImage.map(CIImage.init)?.settingProperties(metadata),
let savedImageURL = ciImage.saveImageWithExif() {
return (image: image, url: savedImageURL)
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment