如何使用 Photos Framework 在 iOS 9 中获取图像元数据
How to get image metadata in iOS 9 using Photos Framework
我想在 Swift 中使用 UIImagePickerController 显示图像元数据。
其中图像是从图库中选择的,将与元数据一起显示在 imageView 中,例如 PixelHeight、PixelWidth、PixelXDimension、PixelYDimension、Coordinates、Size、Date Created 和 Imagename。
首先,您可以从 UIImagePickerController 获取信息[UIImagePickerControllerReferenceURL]。然后您可以执行以下操作:
let assetURL = info[UIImagePickerControllerReferenceURL] as! NSURL
let asset = PHAsset.fetchAssetsWithALAssetURLs([assetURL], options: nil)
guard let result = asset.firstObject where result is PHAsset else {
return
}
let imageManager = PHImageManager.defaultManager()
imageManager.requestImageDataForAsset(result as! PHAsset, options: nil, resultHandler:{
(data, responseString, imageOriet, info) -> Void in
let imageData: NSData = data!
if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary
//now you have got meta data in imageProperties, you can display PixelHeight, PixelWidth, etc.
}
})
我想在 Swift 中使用 UIImagePickerController 显示图像元数据。 其中图像是从图库中选择的,将与元数据一起显示在 imageView 中,例如 PixelHeight、PixelWidth、PixelXDimension、PixelYDimension、Coordinates、Size、Date Created 和 Imagename。
首先,您可以从 UIImagePickerController 获取信息[UIImagePickerControllerReferenceURL]。然后您可以执行以下操作:
let assetURL = info[UIImagePickerControllerReferenceURL] as! NSURL
let asset = PHAsset.fetchAssetsWithALAssetURLs([assetURL], options: nil)
guard let result = asset.firstObject where result is PHAsset else {
return
}
let imageManager = PHImageManager.defaultManager()
imageManager.requestImageDataForAsset(result as! PHAsset, options: nil, resultHandler:{
(data, responseString, imageOriet, info) -> Void in
let imageData: NSData = data!
if let imageSource = CGImageSourceCreateWithData(imageData, nil) {
let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil)! as NSDictionary
//now you have got meta data in imageProperties, you can display PixelHeight, PixelWidth, etc.
}
})