iPhone获取UIImagePickerController Lat / Lng

我正在使用UIImagePickerController拍照,然后将照片上传到WCF Service 。 随着形象,我也需要发送照片的经纬度。

如何从拍摄的照片中获得这些信息?

我在网上search,但似乎无法find一个很好的信息来源。 我已经看到作为didFinishPickingMediaWithInfo委托方法的一部分,通过UIImagePickerControllerMediaMetadata键传递的EXIF数据。 但是,当我将内容打印到日志中时,没有位置信息。

我错过了什么,在拍照之前是否需要打开位置服务? 还是有另一种方式来获取信息?

非常感谢。

我已经搞清楚了几天,终于find了解决办法。 如前所述,您可以使用ALAssetsLibrary

图像select器会给你一个字典,其中包含一个指向资产的url。

ALAssetsLibraryassetForURL: resultBlock: failureBlock:方法使用块。 你可以在这里阅读更多关于他们的例子。 所以这就是我们如何处理select器给出的信息:

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary) { // We'll store the info to use in another function later self.imageInfo = info; // Get the asset url NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"]; // We need to use blocks. This block will handle the ALAsset that's returned: ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset) { // Get the location property from the asset CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation]; // I found that the easiest way is to send the location to another method [self handleImageLocation:location]; }; // This block will handle errors: ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror) { NSLog(@"Can not get asset - %@",[myerror localizedDescription]); // Do something to handle the error }; // Use the url to get the asset from ALAssetsLibrary, // the blocks that we just created will handle results ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease]; [assetslibrary assetForURL:url resultBlock:resultblock failureBlock:failureblock]; } [picker dismissModalViewControllerAnimated:YES]; [picker release]; } 

接下来是处理图像和位置数据的方法:

 - handleImageLocation:(CLLocation *)location { UIImage *image = [self.imageInfo objectForKey:UIImagePickerControllerOriginalImage]; // Do something with the image and location data... } 

当然你也可以通过使用这个键来获得关于图像的其他信息:

 ALAssetPropertyType ALAssetPropertyLocation ALAssetPropertyDuration ALAssetPropertyOrientation ALAssetPropertyDate ALAssetPropertyRepresentations ALAssetPropertyURLs 

自iOS 8以来的新增function

现在已经废弃的ALAssetLibrary变得复杂了,而是使用Photos.Framework

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { if let assertURL = info[UIImagePickerControllerReferenceURL] as? NSURL { let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([assertURL], options: nil) if let asset = fetchResult.firstObject as? PHAsset { print(asset.location) } } } 

快捷方便 :)

这是一个“错误”,我想。 UIImagePickerViewController将不会返回您从中select的图像的位置数据:正如您发现的那样,它们从元数据中被剥离。

但是,如果使用ALAssetLibrary获取图像,则可以获取位置数据(以与图像关联的CLLocation对象的forms)。 这个问题有一些代码 – iphone image ALAsset问题 – 这将帮助你从UIImagePickerController获得一个ALAsset

当然,如果你可以直接从select器中获得这些信息,那么这会更容易,所以考虑向苹果提交一个function请求。

因为你的源types看起来像是UIImagePickerControllerSourceTypeCamera ,所以你很快就会感到非常沮丧。 位置信息似乎是MIA。

作为UIImageWriteToSavedPhotosAlbum() (不参考保存的图像)的替代方法,我已经尝试了ALAssetsLibrary-writeImageToSavedPhotosAlbum:metadata:completionBlock:使用UIImagePickerControllerMediaMetadata来填充它,希望框架将填充位置信息的空白)。

唉,没有这样的运气。 即使这个方法给了一个保存的图像的URL,我仍然不能从它获得ALAssetPropertyLocation信息。 即使我使用保存方法的方向变体,也是没有用的。

在这一点上,我认为唯一的办法就是将您自己的位置信息纳入其中,如下所述 。

这似乎有点可怕,我不得不这样做,但是, 我期望框架自动为您处理这个 。 事实上,我甚至无法获取以前拍摄的照片的位置信息。 这是在所有正确的地方授予的所有权限,无论使用哪种设备,iPhone或iPad。 (即使相机应用程序有适当的权限,FWIW。)