问题描述
我正在使用 UIImagePickerController
拍摄照片,然后将照片上传到 WCF服务
。除了图像,我还需要发送到照片的纬度和经度。
I'm using the UIImagePickerController
to take a photo and then upload the photo to a WCF Service
. Along with the image, I also need to send to the latitude and longitude of the photo.
如何从拍摄的照片中获取此信息?
How can I get hold of this information from the photo taken?
我在网上搜索过,但似乎找不到好的信息来源。我已经看到了 EXIF
数据,这些数据是作为 UIImagePickerControllerMediaMetadata 键传递的> didFinishPickingMediaWithInfo 委托方法。但是,当我将内容打印到日志时,没有位置信息。
I've searched online but can't seem to find a good source of info. I've seen about the EXIF
data that is passed through the UIImagePickerControllerMediaMetadata
key as part of the didFinishPickingMediaWithInfo
delegate method. However, when I print out the contents to the log, there's no location information.
我错过了什么,我是否需要在拍照前打开位置服务?还是有另一种获取信息的方式吗?
Am I missing something, do I need to turn on location services prior to taking the photo? Or is there another way to get the information?
非常感谢。
推荐答案
我已经把这个问题搞砸了几天,最后得到了解决方案。如上所述,您可以使用 ALAssetsLibrary 。
I've been figuring this out for a couple of days, and finally got a solution. As already suggested, you can use ALAssetsLibrary.
图像选择器会为您提供一个字典,其中包含指向资产的网址。
The image picker will give you a dictionary, which contains an url that points to the asset.
ALAssetsLibrary 的 assetForURL:resultBlock:failureBlock:
方法使用块。您可以从示例了解更多相关信息。
所以这就是我们处理选择器给出的信息的方式:
The ALAssetsLibrary's assetForURL: resultBlock: failureBlock:
method uses blocks. You can read more about them from example here.So this is how we handle the info given by the picker:
- (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];
}
接下来是处理图像和位置数据的方法:
And next the method that handles the image and location data:
- handleImageLocation:(CLLocation *)location
{
UIImage *image = [self.imageInfo objectForKey:UIImagePickerControllerOriginalImage];
// Do something with the image and location data...
}
当然,您还可以使用此键通过此方法获取有关图像的其他信息:
And of course you can also get other information about the image with this method by using keys:
ALAssetPropertyType
ALAssetPropertyLocation
ALAssetPropertyDuration
ALAssetPropertyOrientation
ALAssetPropertyDate
ALAssetPropertyRepresentations
ALAssetPropertyURLs
这篇关于iPhone获取UIImagePickerController Lat / Lng的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!