2012-02-15 27 views
7

在我的應用程序中有一個按鈕,用戶只需點擊它即可直接在屏幕上檢索庫中的最新照片。如何獲得庫中的最新照片?如何獲取iPhone庫中的最新照片?


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

我不知道如果這總是工作...希望任何人都可以證明我。

還有我正在尋找一種方式來同步線程...

+0

你可以做'[self.assets lastObject]',它有益返回無如果計數爲0. – 2012-07-21 04:44:23

回答

2

這段代碼可以檢索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]; 
    } 
} 

獲取圖像後,通知會將回叫發送回相關的類。

2

你需要把所有的照片創建的日期,然後按日期排序。

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

謝謝,但如何訪問照片庫並閱讀每張專輯中的所有圖像? – 2012-02-15 11:05:00

+1

http://www.icodeblog.com/2010/07/08/asset-libraries-and-blocks-in-ios-4/ – 2012-02-15 12:12:55

+0

乾杯隊友!!!! – 2012-02-16 05:20:41

11

你可以簡單地通過以下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** 
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"); 
    }]; 
} 
0

的方法,這樣做只是做這個傢伙

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

     }]; 
    } 
相關問題