[英]How to get the latest photo in iPhone Library?
在我的應用程序中有一個按鈕,用戶只需單擊它,然后可以直接在屏幕上檢索庫中的最新照片。 如何在圖書館中獲取最新照片?
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];
要獲取文件日期,我使用它
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];
我不知道如果這總是有效的......希望任何人都可以證明我。
在那里,我正在尋找一種同步線程的方法......
您可以通過遵循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**
此代碼段可以檢索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];
要獲取文件日期,我使用:
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];
我不知道這是否總能奏效......我希望有人能夠澄清我的疑慮。
在那里,我正在尋找一種同步線程的方法......
2012-04-03更新
現在我使用NSNotification
來解決這個問題:
- (void)gotLastImageCallBack {
if (notifyName) {
[[NSNotificationCenter defaultCenter] postNotificationName:notifyName object:nil];
}
}
獲取圖像后,通知會將回調發送回相關類。
您需要將所有照片創建日期,然后按日期對其進行排序。
- (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;
}
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];
}
}];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.