objective-cxcode9phfetchoptions

How do you fetch only favorite pictures using Objective C's PHFetchOptions


I can fetch photos in a certain date range by doing this, but how can I further filter by their favorite statuses?

PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"(creationDate >= %@) && (creationDate <= %@)",startDateMinus24Hours,endDatePlus24Hours];
_assetsFetchResults = [PHAsset fetchAssetsWithOptions:fetchOptions];

I googled for "PHFetchOptions predicate favorites", but couldn't find an answer. If you know the exact answer, or a reference to predicate syntax, please let me know. Thanks!


Solution

  • I figured it out.

    PHFetchOptions *fetchOptions = [PHFetchOptions new];
    NSString *format = @"(creationDate >= %@) && (creationDate <= %@)";
    if (showFavoritePhotosOnly) {
        format = [format stringByAppendingString:@" && (favorite == true)"];
    }
    fetchOptions.predicate = [NSPredicate predicateWithFormat:format,startDateMinus24Hours,endDatePlus24Hours]; //iwashere
    _assetsFetchResults = [PHAsset fetchAssetsWithOptions:fetchOptions];