gpt4 book ai didi

iPhone 获取 UIImagePickerController 纬度/经度

转载 作者:技术小花猫 更新时间:2023-10-29 10:33:48 26 4
gpt4 key购买 nike

我正在使用 UIImagePickerController 拍摄照片,然后将照片上传到 WCF 服务。除了图像,我还需要发送照片的纬度和经度。

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

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

我是否遗漏了什么,是否需要在拍照前打开定位服务?或者有其他获取信息的方法吗?

非常感谢。

最佳答案

我已经弄清楚了几天,终于找到了解决方案。如前所述,您可以使用 ALAssetsLibrary

图像选择器会给你一个字典,其中包含一个指向 Assets 的 url。

ALAssetsLibraryassetForURL: resultBlock: failureBlock: 方法使用 block 。您可以从示例 here 中阅读更多关于它们的信息.这就是我们处理选择器提供的信息的方式:

- (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

关于iPhone 获取 UIImagePickerController 纬度/经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6177606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com