iPhone获取UIImagePickerController Lat/Lng

Dan*_*lis 12 iphone objective-c uiimagepickercontroller ios

我正在使用UIImagePickerController拍照然后将照片上传到WCF Service.随着图像,我还需要发送到照片的纬度和经度.

如何从拍摄的照片中获取此信息?

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

我错过了什么,我是否需要在拍照前打开位置服务?或者有另一种获取信息的方法吗?

非常感谢.

lar*_*ari 15

我已经把这个问题搞砸了几天,最后得到了解决方案.如前所述,您可以使用ALAssetsLibrary.

图像选择器将为您提供一个字典,其中包含指向资产的URL.

所述ALAssetsLibraryassetForURL: resultBlock: failureBlock:方法使用块.您可以在此处阅读有关它们的更多信息.这就是我们处理选择器给出的信息的方式:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

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

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

当然,您也可以使用此键通过此方法获取有关图像的其他信息:

ALAssetPropertyType
ALAssetPropertyLocation
ALAssetPropertyDuration
ALAssetPropertyOrientation
ALAssetPropertyDate
ALAssetPropertyRepresentations
ALAssetPropertyURLs
Run Code Online (Sandbox Code Playgroud)

  • 如果源类型是库,这很好,但是如果它是相机(UIImagePickerControllerSourceTypeCamera)呢?在这种情况下,网址将为零.:( (3认同)