我们将EXIF数据显示为空白。我们使用的是Swift3和iOS10.3 我们引用了网址 - UIImagePickerController and extracting EXIF data from existing photos
但是,它没有解释如何在Swift中实现它。
下面拍照是我们的代码。
拍照
@IBAction func takePhoto(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
获取照片
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
myImg.contentMode = .scaleToFill
myImg.image = pickedImage
}
picker.dismiss(animated: true, completion: nil)
}
保存照片
@IBAction func savePhoto(_ sender: AnyObject) {
let imageData = UIImagePNGRepresentation(myImg.image!)
let compresedImage = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(compresedImage!, nil, nil, nil)
let alert = UIAlertController(title: "Saved", message: "Your image has been saved", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
在此之后,当我们检查Photo的属性时 - EXIF数据为空白。
我已经阅读了与此相关的各种文章,但没有与Swift3相关的答案。 真的很感激,如果有人可以帮助我们。 谢谢。
答案 0 :(得分:2)
回答这个问题有点晚了,但我发现这个剪辑提供了我所需的exif数据。
import Photos
import PhotosUI
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let assetURL = info[UIImagePickerControllerReferenceURL] as! NSURL
let asset = PHAsset.fetchAssets(withALAssetURLs: [assetURL as URL], options: nil)
guard let result = asset.firstObject else {
return
}
let imageManager = PHImageManager.default()
imageManager.requestImageData(for: result , options: nil, resultHandler:{
(data, responseString, imageOriet, info) -> Void in
let imageData: NSData = data! as NSData
if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
let imageProperties2 = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary
print("imageProperties2: ", imageProperties2)
}
})
dismiss(animated: true, completion: nil)
}
希望这有帮助
答案 1 :(得分:1)
如果有人需要从avcapturesession捕获的图像中提取数据。
photoFileOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil) {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer!)
let image = self.processPhoto(imageData!)
let source: CGImageSource = CGImageSourceCreateWithData((imageData as! CFMutableData), nil)!
let metadata = CGImageSourceCopyPropertiesAtIndex(source, 0,nil) as! [String:Any]
print("exif data = \(metadata![kCGImagePropertyExifDictionary as String] as? [String : AnyObject]) ")
completionHandler(true)
} else {
completionHandler(false)
}
}
答案 2 :(得分:1)
**使用Photos API的iOS 13 SWIFT 5:
为此,您将需要该应用访问用户照片库。为此,请添加:
NSPhotoLibraryUsageDescription
到Info.plist并定义为什么需要访问。
然后在显示选择器之前,请求访问权限
import Photos
func openPicker() {
let status = PHPhotoLibrary.authorizationStatus()
let picker = UIImagePickerController()
picker.sourceType = .photoLibrary
if status == .notDetermined {
PHPhotoLibrary.requestAuthorization({status in
if status == .authorized {
self.present(self.picker, animated: true)
}
})
} else if status == .authorized {
present(picker, animated: true)
} else {
print("NO ACCESS")
return
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let asset = info[UIImagePickerController.InfoKey.phAsset] as! PHAsset
PHImageManager.default().requestImageDataAndOrientation(for: asset, options: nil) { (data, responseString, imageOrient, info) in
let imageData: NSData = data! as NSData
if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as! [String: Any]
print("properties: ", imageProperties)
}
}
dismiss(animated: true, completion: nil)
}
答案 3 :(得分:0)
FOR SWIFT 4 +
在didFinishPickingMediaWithInfo中添加以下内容:
let dictionary : NSDictionary = info[UIImagePickerController.InfoKey.mediaMetadata] as! NSDictionary
let exif = dictionary.value(forKey: "{Exif}") as! NSDictionary
let xPixels = exif.value(forKey: "PixelXDimension") as! Int
let yPixels = exif.value(forKey: "PixelYDimension") as! Int
print(exif as Any) // EXIF DICTIONARY
print(xPixels) // exif contents(dimensions)
print(yPixels)