我需要从UIImagePickerController采集的照片中获取GPS数据,并提取用于UIMapKit的照片的位置,我尝试了一些堆栈溢出和Google中的示例代码,但它们对我都没有用!我所使用的照片包含纬度和经度数据,但是它总是返回带这个致命错误的nil:“意外地找到了零,而打开了一个可选的值”,我在模拟器上测试了这个应用程序,它会引起什么问题吗?我考虑过各种各样的问题,我是不是做错了什么?这是代码
func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!) {
if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary {
let url: AnyObject? = info[UIImagePickerControllerReferenceURL] as? NSURL
let library = ALAssetsLibrary()
library.assetForURL(url as NSURL, resultBlock: { (asset: ALAsset!) -> Void in
if asset != nil {
var assetRep: ALAssetRepresentation = asset.defaultRepresentation()
var metaData: NSDictionary = assetRep.metadata()
let location = metaData.objectForKey(ALAssetPropertyLocation) as CLLocation!
let lat = location.coordinate.latitude
let long = location.coordinate.longitude
}
}, failureBlock: {
(error: NSError!) in
NSLog("Error!")
}
)} dismissViewControllerAnimated(true, completion: nil)}发布于 2016-01-29 19:08:52
我找到了解决方案,代码是正确的!但是当我通过拖放将GPS数据从照片中删除时,我会将照片存储到Dropbox中,并通过模拟器的safari下载它们!参考Is it possible to access a photo's geotag metadata from within the simulator?
发布于 2021-04-07 09:17:03
这条路干净多了。
import Photos
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
guard let asset = info[.phAsset] as? PHAsset else { return }
// Do whatever
asset.location?.coordinate.latitude
asset.location?.coordinate.longitude
}https://stackoverflow.com/questions/35089796
复制相似问题