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?
2
Just do this guys
void (^enumerate)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)
{
if ([[group valueForProperty:@"ALAssetsGroupPropertyType"] intValue] == ALAssetsGroupSavedPhotos)
{
NSLog(@"Camera roll");
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if(result != nil){
ALAssetRepresentation *rep = [result defaultRepresentation];
[thumbnailsArray insertObject:rep atIndex:0];
}
}];
}
UIButton *photoAlbum = [[UIButton alloc] init];
[photoAlbum addTarget:self action:@selector(getLatestPhoto:) forControlEvents:UIControlEventTouchDown];
-(void)getLatestPhoto
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupLibrary usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if ([group numberOfAssets] > 0)
{
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets]-1] options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop)
{
}];
}
}
failureBlock: ^(NSError *error)
{
NSLog(@"No Photo");
}];
}
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**
You need to get all the photos created date and then sort them by date.
- (NSDictionary *) attributesForFile:(NSURL *)anURI {
// note: singleton is not thread-safe
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *aPath = [anURI path];
if (![fileManager fileExistsAtPath:aPath]) return nil;
NSError *attributesRetrievalError = nil;
NSDictionary *attributes = [fileManager attributesOfItemAtPath:aPath
error:&attributesRetrievalError];
if (!attributes) {
NSLog(@"Error for file at %@: %@", aPath, attributesRetrievalError);
return nil;
}
NSMutableDictionary *returnedDictionary =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
[attributes fileType], @"fileType",
[attributes fileModificationDate], @"fileModificationDate",
[attributes fileCreationDate], @"fileCreationDate",
[NSNumber numberWithUnsignedLongLong:[attributes fileSize]], @"fileSize",
nil];
return returnedDictionary;
}
This code snippet can retrieve an 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 don't know if this is always works... I hope someone can clear up my doubts about that.
And there I'm looking for a way to sync thread...
2012-04-03 Update
Now I use NSNotification
to solve this problem:
- (void)gotLastImageCallBack {
if (notifyName) {
[[NSNotificationCenter defaultCenter] postNotificationName:notifyName object:nil];
}
}
After fetched image, the notification sends call back to related class.