经过建立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;
//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);
}
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