ALAssetsLibrary获取所有图片

通过创建ALAssetsLibrary 的实例可以访问系统Photos里的图片与视频。这个库包括了保存的图片,从iTunes导入的和从其他设备里面导入的图片,你可以访问所有的分类和保存图片视频。

 

用普通的alloc和init就可以创建ALAssetsLibrary的实例,所有你从这个实例中获取得到的对象的生命周期和ALAssetsLibrary这个实例的生命周期一致。

 

大多数的函数,都是异步处理的,所有的成功失败的回调都是使用block传入的,因为有可能需要用户点击授权。在iOS4至iOS5的版本中,会提示要访问用户的地理位置。到iOS6会提示访问用户的照片信息,这一点需要注意。

 

可以遍历Group,创建Group,创建图片,添加信息等,可以进行大多数的访问与操作。

 

可以通过valueForProperty获取到图片的信息,包括类型, Location , 时长,方向,日期,格式 , URL地址。

                             NSString * nsALAssetPropertyType = [ asset valueForProperty:ALAssetPropertyType ] ;

                             NSString * nsALAssetPropertyLocation = [ asset valueForProperty:ALAssetPropertyLocation ] ;

                             NSString * nsALAssetPropertyDuration = [ asset valueForProperty:ALAssetPropertyDuration ] ;

                             NSString * nsALAssetPropertyOrientation = [ asset valueForProperty:ALAssetPropertyOrientation ] ;

                             NSString * nsALAssetPropertyDate = [ asset valueForProperty:ALAssetPropertyDate ] ;

                             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


你可能感兴趣的:(ALAssetsLibrary获取所有图片)