How to get latitude and longitude of an image using PHAsset in iOS?

家住魔仙堡 提交于 2019-12-24 08:48:47

问题


I am trying to fetch images from photo library within the range of two dates and I am getting the images successfully.

And I am also getting the info of the image by using below code.

  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);


        }];

But image info giving me in string format.

I tried to convert it into NSDictionary format(for get lat and long) but getting null output.

How can get lat and long of image ?

if there is an another way to get info please help me

Thank you in advance


回答1:


You don't need to do anything else if you already have the PHAssets (As you say, you have successfully retrieved them).

What you need is .location property of your PHAsset. It is basically an instance of CLLocation. You can use it like:

asset.location.coordinate.longitude

or

asset.location.coordinate.latitude

Since you have already gotten your PHFetchResult successfully, now you need to iterate over it to get PHAsset object and then get location info from it if available. To get GPS Info from PHAsset in form of dictionary, add this method:

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

Now use this method like:

NSMutableDictionary *coordinatesDictionary = [self  getGPSInformationForAsset:asset];

That's it, You got the Latitude and Longitude in a dictionary.



来源:https://stackoverflow.com/questions/39744762/how-to-get-latitude-and-longitude-of-an-image-using-phasset-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!