In my app there is a button, user just click it then the latest photo in library can be retrieved on screen directly. How to get the latest photo in library?
2012/02/17
this can get ALAsset
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != nil)
{
[self.assets addObject:result];
}
};
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil)
{
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:assetEnumerator];
}else{
self.image = [self getLastImage];
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
NSLog(@"error occour =%@", [myerror localizedDescription]);
};
assets = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:assetGroupEnumerator failureBlock:failureblock];
[assetsLibrary release];
To get file date I use this
assetURLArray = [[NSMutableArray alloc] init];
for (ALAsset *asset in self.assets) {
NSDate * date = [asset valueForProperty:ALAssetPropertyDate];
Then I found that the latest image always be the top one of assetURLArray, so I finally get the latest one like this
if (self.assets && [self.assets count]) {
ALAsset *asset = [self.assets objectAtIndex:([self.assets count] - 1)];
CGImageRef ref = [[asset defaultRepresentation]fullResolutionImage];
I donno if this is always work... hope any one can prove me.
And there I'm looking for a way to sync thread...
Tap Settings > [your name] > iCloud > Photos. Make sure Upload to My Photo Stream is turned on. If you don't see this option, turn on iCloud Photos to keep your photos and videos in iCloud instead.
To change the sorting order there, launch iPhoto, select Events, and then choose View -> Sort Events. You can then sort Events by date (in chronological or reverse-chronological order), alphabetically by name, or in a manual order of your own creation.
The Recents album is showing you the photos added to your library sorted by the date they have been added. The All Photos album is showing you all photos in your library, sorted by the date they have been taken.
You can do it simply by following method of ALAssetsGroup
(void)enumerateAssetsWithOptions:(NSEnumerationOptions)options usingBlock:(ALAssetsGroupEnumerationResultsBlock)enumerationBlock;**
applying **NSEnumerationReverse** option
void (^assetEnumerator)(ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
{
if(result != nil)
{
[self.assets addObject:result];
}
};
void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if(group != nil)
{
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:assetEnumerator];
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
NSLog(@"error occour =%@", [myerror localizedDescription]);
};
assets = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:assetGroupEnumerator failureBlock:failureblock];
[assetsLibrary release];
**Your self.assets array will have latest images**
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