Chandan Reddy Chandan Reddy - 2 months ago 25
Objective-C Question

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

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

Answer

You don't need to do anything else if you already have the PHAssets (As you say, you 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.