我需要从图像中提取经度和纬度的Iphone应用程序,除了获取gps数据之外,所有内容都工作到目前为止。我在imagePickerController中有这段代码:
- (UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library1 = [[ALAssetsLibrary alloc] init];
[library assetForURL:referenceURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
NSDictionary *metadata = rep.metadata;
NSLog(@"%@", metadata);
CGImageRef iref = [rep fullScreenImage] ;
if (iref) {
self.myPicture.image = [UIImage imageWithCGImage:iref];
}
} failureBlock:^(NSError *error) {
// error handling
}];
}
输出:
...
Sharpness = 2;
ShutterSpeedValue = "9.710661431591664";
SubjectArea = (
1295,
967,
699,
696
);
WhiteBalance = 0;
};
"{GPS}" = {
Altitude = "144.8338028169014";
AltitudeRef = 0;
DateStamp = "2013:09:07";
ImgDirection = "243.4423676012461";
ImgDirectionRef = T;
Latitude = "37.97166666666666";
LatitudeRef = N;
Longitude = "23.72733333333333";
LongitudeRef = E;
TimeStamp = "08:10:30";
};
.....
我怎样才能获得经度和纬度并加入NSString? 谢谢
答案 0 :(得分:5)
使用CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
可以帮助您从捕获的照片中获取lat和long检查以下代码并使用您的要求。
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"url %@",info);
if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary) {
// Get the asset url
NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
NSLog(@"url %@",url);
// 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.lat =location.coordinate.latitude; //[[gpsdata valueForKey:@"Latitude"]floatValue];
self.lng =location.coordinate.longitude;
NSLog(@"\nLatitude: %f\nLongitude: %f",self.lat,self.lng);
strLocation=[NSString stringWithFormat:@"La:%f Lo%f",self.lat,self.lng];
NSLog(@"Can not get asset - %@",strLocation);
};
// This block will handle errors:
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror)
{
NSLog(@"Can not get asset - %@",[myerror localizedDescription]);
// Do something to handle the error
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:url
resultBlock:resultblock
failureBlock:failureblock];
}
[self dismissViewControllerAnimated:YES completion:^{
}];
}