如何在Swift中获取从UIImagePickerController中选择的照片的CLLocation?

时间:2016-06-26 09:24:15

标签: swift uiimagepickercontroller cllocation phasset

UIImagePickerController从用户的照片库

中选择一张图片

我想在Swift中获取所选照片的​​位置坐标...

我尝试过尝试获取照片的网址....

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
    let imageURL = editingInfo![UIImagePickerControllerReferenceURL] as! NSURL
    self.imageFromAsset(imageURL)
}

并将其传递给......

func imageFromAsset(nsurl: NSURL) {
    let asset = PHAsset.fetchAssetsWithALAssetURLs([nsurl], options: nil).firstObject as! PHAsset
    var location = asset.location   //then do something with this data
}

但是开始使用图片网址会产生致命错误。

希望有人可以提供帮助,谢谢。

错误讯息:

fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) 

此行似乎出现此错误:

    let imageURL = editingInfo![UIImagePickerControllerReferenceURL] as! NSURL

1 个答案:

答案 0 :(得分:0)

使用:

a

而不是:

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

因为它在iOS 3.0中已弃用

https://developer.apple.com/reference/uikit/uiimagepickercontrollerdelegate/1619152-imagepickercontroller