不推荐使用ALAsset - 使用Photos Framework中的PHAsset

时间:2016-04-08 05:48:26

标签: swift metadata uiimagepickercontroller alassetslibrary phphotolibrary

我正在使用

检索图像
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) }
      let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
}

我可以检索图像,也可以使用图像输入按钮背景或我想要的任何其他内容,但我似乎无法检索照片的元数据。现在,关于使用ALAssets和他们的图像数据技术的互联网上的各种其他位置有很多话题,但是,最近已经弃用了ALAssets。由于我的图像不是用户刚拍摄的图像,而是从相机胶卷中选择的图像,我无法使用:

let metadata = info[UIImagePickerControllerMediaMetaData] as! NSDictionary

因此,如果有人能够指出我如何获取用户从相机胶卷中选择的图像元数据的正确方向,而不使用ALAssets,我们将不胜感激!谢谢!

2 个答案:

答案 0 :(得分:6)

如果您想从相机胶卷获取图像,那么这将有所帮助:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

let imageUrl = info["UIImagePickerControllerReferenceURL"]
let asset = PHAsset.fetchAssetsWithALAssetURLs([imageUrl], options: nil).firstObject as! PHAsset
print("location: " + String(asset.location))
print("Creation Date: " + String(asset.creationDate))

}

答案 1 :(得分:1)

来自Muhammad Yawar Ali的Objective-C解决方案:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSString * imageUrl = info[@"UIImagePickerControllerReferenceURL"];
    PHAsset * asset = [[PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:imageUrl] options:nil]firstObject];
    NSLog(@"location: %@",asset.location);
    NSLog(@"Creation Date: %@",asset.creationDate);
}