如何获取iPhone库中的最新照片?

在我的应用程序有一个button,用户只需点击它,然后库中的最新照片可以直接在屏幕上检索。 如何获得图书馆最新的照片?


2012/02/17

这可以得到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]; 

为了得到文件date,我使用这个

  assetURLArray = [[NSMutableArray alloc] init]; for (ALAsset *asset in self.assets) { NSDate * date = [asset valueForProperty:ALAssetPropertyDate]; 

然后我发现最新的图像一直是assetURLArray中的第一个,所以我终于得到了最新的一个

 if (self.assets && [self.assets count]) { ALAsset *asset = [self.assets objectAtIndex:([self.assets count] - 1)]; CGImageRef ref = [[asset defaultRepresentation]fullResolutionImage]; 

我不,如果这总是工作…希望任何人都可以certificate我。

在那里我正在寻找一种方法来同步线程…

您可以简单地通过遵循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** 

你需要获得所有的照片创builddate,然后按datesorting。

 - (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; } 

这段代码可以检索一个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]; 

要获取文件date,我使用这个:

 assetURLArray = [[NSMutableArray alloc] init]; for (ALAsset *asset in self.assets) { NSDate * date = [asset valueForProperty:ALAssetPropertyDate]; 

然后我发现最新的图像总是最重要的一个,所以我最终得到了这样一个最新的图像:

 if (self.assets && [self.assets count]) { ALAsset *asset = [self.assets objectAtIndex:([self.assets count] - 1)]; CGImageRef ref = [[asset defaultRepresentation] fullResolutionImage]; 

我不知道这是否总是有效的…我希望有人能够清除我的怀疑。

在那里我正在寻找一种方法来同步线程…


2012-04-03更新

现在我使用NSNotification来解决这个问题:

 - (void)gotLastImageCallBack { if (notifyName) { [[NSNotificationCenter defaultCenter] postNotificationName:notifyName object:nil]; } } 

获取图像后,通知将发回回相关的类。

 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"); }]; } 

只要做这个家伙

 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]; } }]; }