Gao Gao - 1 year ago 67
iOS Question

Get today's photo from album in IOS

Is there any way to get only today's photos from album in ios´╝č
I know how to get album but all photos displayed as timeline.
I want to get only today's photos or last two days' photo, how can I realized that?

Answer Source

You can use this snippet to get today's photo, which works on iOS 8. I originally filtered the assets from Recently Added album, which stores photos from last 30 days or 1000 photos. There is a chance user captures more than 1000 photos in two days, so I changed the code to get all photos from the library.

PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];

PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsWithOptions:options];

//get day component of today
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents *dayComponent = [calendar components:NSCalendarUnitDay fromDate:[NSDate date]];
NSInteger currentDay =;

//get day component of yesterday = - 1;
NSDate *yesterdayDate = [calendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
NSInteger yesterDay = [[calendar components:NSCalendarUnitDay fromDate:yesterdayDate] day];

//filter assets of today and yesterday add them to an array.
NSMutableArray *assetsArray = [NSMutableArray array];
for (PHAsset *asset in assetsFetchResult) {
    NSInteger assetDay = [[calendar components:NSCalendarUnitDay fromDate:asset.creationDate] day];

    if (assetDay == currentDay || assetDay == yesterDay) {
        [assetsArray addObject:asset];
    else {
        //assets is in descending order, so we can break here.

Prior iOS 8, using ALAssetsLibrary, suppose you have a photo group, enumerate the group in reverse order, and do the similar thing as above.

[self.photoGroup enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
      NSDate *date = [asset valueForProperty:ALAssetPropertyDate];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download