ALAssetsLibrary獲取全部圖片

經過建立ALAssetsLibrary 的實例能夠訪問系統Photos裏的圖片與視頻。這個庫包括了保存的圖片,從iTunes導入的和從其餘設備裏面導入的圖片,你能夠訪問全部的分類和保存圖片視頻。html

 

用普通的alloc和init就能夠建立ALAssetsLibrary的實例,全部你從這個實例中獲取獲得的對象的生命週期和ALAssetsLibrary這個實例的生命週期一致。ios

 

大多數的函數,都是異步處理的,全部的成功失敗的回調都是使用block傳入的,由於有可能須要用戶點擊受權。在iOS4至iOS5的版本中,會提示要訪問用戶的地理位置。到iOS6會提示訪問用戶的照片信息,這一點須要注意。app

 

能夠遍歷Group,建立Group,建立圖片,添加信息等,能夠進行大多數的訪問與操做。iphone

 

能夠經過valueForProperty獲取到圖片的信息,包括類型, Location , 時長,方向,日期,格式 , URL地址。異步

                             NSString * nsALAssetPropertyType = [ asset valueForProperty:ALAssetPropertyType ] ;函數

                             NSString * nsALAssetPropertyLocation = [ asset valueForProperty:ALAssetPropertyLocation ] ;url

                             NSString * nsALAssetPropertyDuration = [ asset valueForProperty:ALAssetPropertyDuration ] ;spa

                             NSString * nsALAssetPropertyOrientation = [ asset valueForProperty:ALAssetPropertyOrientation ] ;.net

                             NSString * nsALAssetPropertyDate = [ asset valueForProperty:ALAssetPropertyDate ] ;code

                             NSString * nsALAssetPropertyRepresentations = [ asset valueForProperty:ALAssetPropertyRepresentations ] ;

                             NSString * nsALAssetPropertyURLs = [ asset valueForProperty:ALAssetPropertyURLs ] ;

                             NSString * nsALAssetPropertyAssetURL = [ asset valueForProperty:ALAssetPropertyAssetURL ] ;

 

 

獲取所有圖片的示例:

//View Controller header(.h) file..

#import <UIKit/UIKit.h>

#include<AssetsLibrary/AssetsLibrary.h> 

 

@interface  getPhotoLibViewController : UIViewController

{

 ALAssetsLibrary *library;

 NSArray *imageArray;

 NSMutableArray *mutableArray;

}

 

-(void)allPhotosCollected:(NSArray*)imgArray;

 

@end

 

 

//implementation file

declare global count variable as

static int count=0;

 

@implementation getPhotoLibViewController

 

-(void)getAllPictures

{

 imageArray=[[NSArray alloc] init];

 mutableArray =[[NSMutableArray alloc]init];

 NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

 

 library = [[ALAssetsLibrary alloc] init];

 

 void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {

  if(result != nil) {

   if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {

    [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

 

    NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

 

    [library assetForURL:url

             resultBlock:^(ALAsset *asset) {

              [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

 

              if ([mutableArray count]==count)

              {

               imageArray=[[NSArray alloc] initWithArray:mutableArray];

               [self allPhotosCollected:imageArray];

              }

             }

            failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ]; 

 

   } 

  }

 };

 

 NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

 

 void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {

  if(group != nil) {

   [group enumerateAssetsUsingBlock:assetEnumerator];

   [assetGroups addObject:group];

   count=[group numberOfAssets];

  }

 };

 

 assetGroups = [[NSMutableArray alloc] init];

 

 [library enumerateGroupsWithTypes:ALAssetsGroupAll

                        usingBlock:assetGroupEnumerator

                      failureBlock:^(NSError *error) {NSLog(@"There is an error");}];

}

 

-(void)allPhotosCollected:(NSArray*)imgArray

{

 //write your code here after getting all the photos from library...

 NSLog(@"all pictures are %@",imgArray);

}

 

@end

Use getAllPicture method to get photos from photo library.

 

 

參考文章:

1.ALAssetsLibrary Class Reference , https://developer.apple.com/library/ios/#documentation/AssetsLibrary/Reference/ALAssetsLibrary_Class/Reference/Reference.html

2. Get all of the pictures from an iPhone photoLibrary in an array using AssetsLibrary framework? -StackOverflow  http://stackoverflow.com/questions/12633843/get-all-of-the-pictures-from-an-iphone-photolibrary-in-an-array-using-assetslibr

相關文章
相關標籤/搜索