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 中? 谢谢
最佳答案
使用CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
可以帮助您从拍摄的照片中获取纬度和经度,检查下面的代码并根据您的要求使用。
- (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:^{
}];
}
关于ios - 来自元数据的 ALAsset 经度和纬度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22268087/