How to show a photo album from newest to oldest
I have a problem when I use the below code to download photo album from iphone. Although I loaded fine, the first photo appeared in the oldest photo. Is it possible to change the order so that I can upload the most recent photo followed by the time and the oldest from the back?
- (IBAction)imageFromAlbum:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagePicker animated:YES completion:nil];
}
source to share
To achieve what you want, you will have to create your own photo pickers yourself using the resource library infrastructure. depending on the version of ios you are using you can use UICollectionViewController (ios6 onwards) or you will have to create your own.
To get all the photos / objects in the saved photos album, follow this method and then sort the objects using their creation date in descending order using the ALAssetPropertyDate property
thisVC.assetArray
will be the data source for your custom table controller or collector. These two methods are asynchronous, so you will need to update the tableView or collectionView after you finish creating the data source
__block YourViewController *thisVC = self;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[self enumerateAssetForGroup:group forFilter:[ALAssetsFilter allPhotos] withCompletionBlock:^(id object) {
thisVC.assetArray = object;
[thisVC.assetArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDate *date1 = [obj1 valueForProperty:ALAssetPropertyDate];
NSDate *date2 = [obj2 valueForProperty:ALAssetPropertyDate];
return ([date1 compare:date2] == NSOrderedAscending ? NSOrderedDescending : NSOrderedAscending);
}];
//in case of table
[thisVC.tableView reloadData];
//in case of collection view
//reload collection view controller data
}];
}
} failureBlock:nil];
- (void)enumerateAssetForGroup:(ALAssetsGroup*)group forFilter:(ALAssetsFilter*)filter withCompletionBlock:(ALAssetsEnumeration)enumerationCompletionBlock {
[group setAssetsFilter:filter];
__block NSInteger assetsCount = [group numberOfAssets];
__block NSMutableArray *assetArray = [[NSMutableArray alloc] init];
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
[assetArray addObject:result];
if (*stop) {
enumerationCompletionBlock(assetArray);
[assetArray release];
}
}
else if (assetsCount == 0) {
enumerationCompletionBlock(nil);
}
}];
}
this part written in the first method will sort your array in descending order,
[thisVC.assetArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDate *date1 = [obj1 valueForProperty:ALAssetPropertyDate];
NSDate *date2 = [obj2 valueForProperty:ALAssetPropertyDate];
return ([date1 compare:date2] == NSOrderedAscending ? NSOrderedDescending : NSOrderedAscending);
}];
Try this if you really want to :)
source to share