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...
See Question&Answers more detail:os