UIImagePickerControllerReferenceURL 返回 nil,导致应用程序崩溃

Posted

技术标签:

【中文标题】UIImagePickerControllerReferenceURL 返回 nil,导致应用程序崩溃【英文标题】:UIImagePickerControllerReferenceURL returns nil, crashing the app 【发布时间】:2014-11-07 13:34:03 【问题描述】:

我正在尝试在 imagePickerController: didFinishPickingMediaWithInfo 函数中打印所有图像元数据。当我使用 info.objectForKey(UIImagePickerControllerReferenceURL) 方法时,它返回 nil,如果尝试使用此结果,我的应用程序将崩溃。有谁知道为什么它返回 nil 以及当我选择图像时我还能用什么来打印所有图像元数据? (使用 UIImageJPEGRepresentation 不是一个选项,因为 EXIF 数据已被删除)。 这是我的代码:

func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info:     NSDictionary!)

let image = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage

let refURL : NSURL = info.objectForKey(UIImagePickerControllerReferenceURL) as NSURL
var localSourceRef: CGImageSourceRef = CGImageSourceCreateWithURL(refURL, nil)
var localMetadata: NSDictionary = CGImageSourceCopyPropertiesAtIndex(localSourceRef, 0, nil)
println("\n Photo data: \n")
println(localMetadata) 

【问题讨论】:

UIImagePickerControllerReferenceURL 在几种情况下会为零,最常见的是来自相机(相对于相机胶卷),因为图像尚未保存。你能补充一些关于你到目前为止尝试过的测试用例的额外信息吗? 没错,它适用于相机胶卷。但如果照片是用相机拍摄的,或者我尝试从照片库访问照片,它会崩溃。您对我如何从照片中获取 gps 数据有什么建议吗?谢谢! 【参考方案1】:

所以听起来这里实际上有两个问题:

1) 为什么 UIImagePickerControllerReferenceURL 返回一个 nil 引用?

2) 如何从照片中获取位置数据?

因此,(1)的答案通常是因为您在操作系统将文件写入图像库之前收到回调 didFinishPickingMedia。

#2 的答案要复杂得多,正如该问题的一系列答案所示: Reading the GPS data from the image returned by the camera in ios iphone

您需要考虑许多变量:

如果您没有请求访问位置数据,iOS 会删除 GPS 数据,因此您需要使用 CLLocationManager 提示您访问位置。 如果用户禁用了地理标记,您将永远无法获得 GPS 坐标。 如果手机无法获得 GPS 锁定,iOS 将不会记录 GPS 坐标。

根据这个答案:https://***.com/a/10338012/490180 您应该能够检索原始 UIImage,然后从 UIImage 的 CGImage 的数据属性创建 CGImageSourceRef。这有效地消除了您访问 ReferenceURL 的需要。

【讨论】:

谢谢你,我听从了你的建议,并按照你所说的为照片分配了位置。如果你愿意帮忙,我还有一个问题***.com/questions/26760410/…

以上是关于UIImagePickerControllerReferenceURL 返回 nil,导致应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章