Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get today's photo from album in IOS

Tags:

ios

photo

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? Thanks.

like image 317
Gao Avatar asked Oct 12 '14 01:10

Gao


People also ask

How do I use the recents album on my iPhone?

The Recents album shows your entire collection in the order that you added them to your library. When you use iCloud Photos, the changes that you make to your albums on one device appear on your other devices too. Open Photos. Go to Albums and tap the Add button .

How do I create a photo album on my iPhone?

On your iPhone, iPad, or iPod touch: Go to Albums and tap the Add button . Enter an album name, then choose the photos and videos that you want to organize in the new album. On your Mac: Choose File > New Album. Enter an album name, then click Photos in the sidebar.

How can I view my old photos on my iPhone?

Update your iPhone, iPad, or iPod touch to the latest version of iOS or iPadOS. Set up iCloud on all your devices. Make sure that you’re signed in with the same Apple ID. Enjoy a curated view of your moments in the Library tab, then browse by years, months, days, or all photos.

How do I sort photos in an album on Android?

Go to an album, then tap the More button . Tap Sort, then choose a sorting option, like Custom Order, Oldest to Newest, or Newest to Oldest. You can share photos, videos, and albums with select people, then allow them to add their own photos, videos, and comments.


3 Answers

Swift 3 version with a range of dates:

let fromDate = // the date after which you want to retrieve the photos
let toDate // the date until which you want to retrieve the photos 

let options = PHFetchOptions()
options.predicate = NSPredicate(format: "creationDate > %@ && creationDate < %@", fromDate as CVarArg, toDate as CVarArg)

//Just a way to set order
let sortDescriptor = NSSortDescriptor(key: "creationDate", ascending: false)
options.sortDescriptors = [sortDescriptor]

return PHAsset.fetchAssets(with: .image, options: options)
like image 200
Nuno Gonçalves Avatar answered Oct 06 '22 01:10

Nuno Gonçalves


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 = dayComponent.day;

//get day component of yesterday
dayComponent.day = - 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.
        break;
    }
}

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];
  }];
like image 38
gabbler Avatar answered Oct 05 '22 23:10

gabbler


You could use a predicate for the day

PHFetchOptions *allPhotosOptions = [PHFetchOptions new];
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];

NSPredicate *predicateMediaType = [NSPredicate predicateWithFormat:@"mediaType = %d",PHAssetMediaTypeImage];
NSDate *date = [[NSDate date] beginningOfDay];
NSPredicate *predicateDate = [NSPredicate predicateWithFormat:@"creationDate >= %@", date];

NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicateDate, predicateMediaType]];


allPhotosOptions.predicate = compoundPredicate;

PHFetchResult *allPhotosResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:allPhotosOptions];

Where

@implementation NSDate (Utils)

- (NSDate *)beginningOfDay {
NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [calendar components:CYCalendarUnitYear | CYCalendarUnitMonth | CYCalendarUnitDay fromDate:self];

    return [calendar dateFromComponents:components];
}
like image 34
Ryan Heitner Avatar answered Oct 05 '22 23:10

Ryan Heitner