在Swift #3中获取拍摄图像的EXIF元数据

8
我们获取的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)
    }

在此之后,当我们检查照片的属性时 - EXIF数据为空。

我已经阅读了各种相关文章,但没有关于Swift3的答案。 如果有人能够帮助我们,我们将不胜感激。 谢谢。


感谢您的输入,但是我们已经查看了这个URL,它没有包含任何与Swift3相关的答案。 - Banng
4个回答

5

虽然有点晚,但我发现这段代码提供了我需要的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)
    }

希望这有所帮助。

2

如果有人需要从通过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

**iOS 13, SWIFT 5,使用Photos API:

为了使此功能正常工作,您需要让应用程序访问用户的照片库。要做到这一点,请添加:NSPhotoLibraryUsageDescription到Info.plist中,并定义您需要访问的原因。

enter image description here

然后在显示选择器之前,请请求访问权限。

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)
}

1
如果您能在特定更改上添加注释以适应iOS 13和Swift 5,那将非常好。 - Simas Joneliunas

1

适用于 Swift 4+

在 didFinishPickingMediaWithInfo 中添加以下内容:

 let dictionary : NSDictionary = info[UIImagePickerController.InfoKey.mediaMetadata] 


    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)

如何获取日期时间,如果它不在字典中,该怎么添加? - Neeraj Joshi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接