如何在iOS中使用PHAsset获取图像的纬度和经度?

时间:2016-09-28 10:23:25

标签: ios objective-c uiimage nsdictionary phasset

我正在尝试在两个日期I am getting the images successfully的范围内从照片库中提取图片。

我还使用下面的代码获取图像信息。

  PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
        options.networkAccessAllowed = YES; //download asset metadata from iCloud if needed

        [asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
            CIImage *fullImage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];

            //NSLog(@"fullImage=%@", fullImage.properties.description);


        }];

但是图片信息以字符串格式提供给我。

我尝试将其转换为NSDictionary格式(用于获取lat和long)但获得null输出。

如何获得lat和long图像?

如果有其他方式获取信息,请帮助我

提前谢谢

1 个答案:

答案 0 :(得分:2)

如果您已经拥有PHAssets,那么您不需要做任何其他事情(正如您所说,您已成功检索到它们)。

您需要的是PHAsset的.location属性。它基本上是CLLocation的一个实例。您可以像使用它一样使用它:

asset.location.coordinate.longitude

asset.location.coordinate.latitude

由于您已成功获得PHFetchResult,现在需要对其进行迭代以获取PHAsset对象,然后从中获取位置信息(如果可用)。要以字典的形式从PHAsset获取GPS信息,请添加以下方法:

-(NSMutableDictionary *) getGPSInformationForAsset: (PHAsset *) asset
{
    NSString *longitude = [NSString stringWithFormat:@"%f",asset.location.coordinate.longitude];
    NSString *latitude = [NSString stringWithFormat:@"%f",asset.location.coordinate.latitude];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:longitude forKey:@"longitude"];//Perform proper validation here for whatever your requirements are
    [dic setValue:latitude forKey:@"latitude"];
    return dic;
}

现在使用以下方法:

NSMutableDictionary *coordinatesDictionary = [self  getGPSInformationForAsset:asset];

那就是它,你在字典中得到了纬度和经度。