我想从 PhotoLibrary 中的图像获取 EXIF 数据,但找不到正确的方法。我使用了这个问题和这个问题的答案。我得到以下控制台输出:
imageSource: <UIImage: 0x6080000b8a20> size {3000, 2002} orientation 0 scale 1.000000
2017-09-18 11:24:28.513567+0300 PhotoTest[10581:6526935] CGImageSourceCopyPropertiesAtIndex:3517: *** ERROR: CGImageSourceCopyPropertiesAtIndex: source is not a CGImageSourceRef
2017-09-18 11:24:29.071412+0300 PhotoTest[10581:6527417] [discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}
这是我当前的代码尝试。我很感激任何解决方案或想法。我一直在摸索各种方法,但我对
imageIO
的了解还不够,无法解决问题。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: nil)
let bigImage = info[UIImagePickerControllerOriginalImage] as? UIImage
photoImageView.image = bigImage
imageData = UIImagePNGRepresentation(bigImage!) as NSData?
if let imageSource = info[UIImagePickerControllerOriginalImage] {
print("imageSource: ", imageSource)
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource as! CGImageSource, 0, nil)
//print("imageProperties: ", imageProperties!)
if let dict = imageProperties as? [String: Any] {
print(dict)
}
}
dismiss(animated: true, completion: nil)
}
您正在看到:
*** ERROR: CGImageSourceCopyPropertiesAtIndex: source is not a CGImageSourceRef
行,因为您需要通过
imageSource
API 之一创建 CGImageSourceCreate...
。
尝试这样做:
if let bigImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
if let imageData = UIImagePNGRepresentation(bigImage)
{
if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil)
{
print("imageSource: ", imageSource)
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)
//print("imageProperties: ", imageProperties!)
if let dict = imageProperties as? [String: Any] {
print(dict)
}
}
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let exifData = info[.mediaMetadata] as? [String: Any] {
//print(exifData)
}
}
我找到了不同的解决方案,如下:
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)
}