Frederick C. Lee
Frederick C. Lee

Reputation: 9513

How do I fetch photos per Geo Location?

I'm assuming that I should use 'PHFetchOptions' to formulate a predicate to fetch photos (PHAssets) per EXIF tags (Geo location or 'tags').

How would I do that?
...or, is there a better way?

Example: fetch photos taken within San Francisco's latitude/longitude from the photo library.

Upvotes: 0

Views: 393

Answers (1)

Martino Bonfiglioli
Martino Bonfiglioli

Reputation: 1577

You cannot, as specified in the documentation:

Construct a predicate with the properties of the class of photo entities that you want to fetch, listed in Table 1: SELF, localIdentifier, creationDate, modificationDate, mediaType, mediaSubtypes, duration, pixelWidth, pixelHeight, favorite (or isFavorite), hidden (or isHidden), burstIdentifier

You have to fetch all photos in library and filter the result after:

  if ([PHPhotoLibrary authorizationStatus]!=PHAuthorizationStatusDenied) {
    PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:nil];
    [allPhotosResult enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        PHAsset *photo = obj;
        if (photo.location) {
            [photos addObject:photo];
        }
        if (allPhotosResult.count-1 == idx) {
            [self showPhotosOnMap];
        }
    }];
}

Upvotes: 2

Related Questions