ios - 来自元数据的 ALAsset 经度和纬度

标签 ios objective-c uiimagepickercontroller alassetslibrary alasset

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/

相关文章:

ios - 创建 UIActionSheet

objective-c - iOS 中带有结构的 XML-RPC 请求

iphone - didFinishPickingMediaWithInfo中的应用程序崩溃

ios - UITableViewHeaderFooterView 外观

ios - 如何知道您的应用程序是事件的还是空闲的?

ios - 当我从另一个类调用方法时 tableView reloadData 不工作

ios - uiimagepickercontroller调用时,状态栏会返回

ios - 可以为 iOS 设计 Google Maps SDK 的样式吗?

iphone - 如何将 nil-terminated 字符串列表传递给 Objective-C 函数?

iPhone 自定义相机覆盖(加图像处理): how-to