iosobjective-cperformancephotosfetching-strategy

Fetch batch of iOS pictures


I would like to fetch fixed batches of the pictures located on an iOS device. I am using the new Photos Framework, and I already found this workaround:

PHFetchOptions *allPhotosOptions = [PHFetchOptions new];

allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];

// Fetches all photos -> i would like to fetch only some of them
PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(currentIndex, batch_size)];

// Iterates over a subset of the previously fetched photos
[allPhotosResult enumerateObjectsAtIndexes:indexSet options:0 usingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop)
{
    // Do stuff    
}

It works fine, but I first fetch all photos with fetchAssetsWithMediaType and then select an subset of the result for my app to load, which seems quite heavy...

I wanted to know if there was a way to directly fetch photos batch by batch, and not fetch them all then iterate. Furthermore, it would be perfect if I could keep the indexes of the last fetched batch, to know where I'll fetch the next batch.

Thanks for your help!


Solution

  • After a more detailed search, I finally found here what I was looking for : One just could use setFetchBatchSize from NSSortDescriptor.