I'm having a hard figuring out how to programmatically retrieve the most recent photo in the camera roll without user intervention. To be clear, I do not want to use an Image Picker, I want the app to automatically grab the newest photo when the app opens.
I know this is possible because Ive seen a similar app do it, but I cant seem to find any info on it.
Connect your new iPhone and open iTunes (or Finder if you are using macOS Catalina or higher). Select your device in iTunes or Finder > Click Photos > Check the option to Sync Photos > Select the folder on your computer you wish to copy photos from > Click Apply.
You can filter and sort photos and videos in the albums you create in the Photos app . For example, you can filter an album to show only videos, only photos, or photos and videos you marked as favorites. You can also sort photos and videos in an album by newest to oldest, oldest to newest, or in a custom order.
One way is to use AssetsLibrary and use n - 1 as the index for enumeration.
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init]; [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) { if (nil != group) { // be sure to filter the group so you only get photos [group setAssetsFilter:[ALAssetsFilter allPhotos]]; if (group.numberOfAssets > 0) { [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:group.numberOfAssets - 1] options:0 usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { if (nil != result) { ALAssetRepresentation *repr = [result defaultRepresentation]; // this is the most recent saved photo UIImage *img = [UIImage imageWithCGImage:[repr fullResolutionImage]]; // we only need the first (most recent) photo -- stop the enumeration *stop = YES; } }]; } } *stop = NO; } failureBlock:^(NSError *error) { NSLog(@"error: %@", error); }];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With