加入AssetsLibrary.framework

然后引入


#import <AssetsLibrary/ALAssetsLibrary.h>


一个获取全部图片的类

.h

#import <UIKit/UIKit.h>
#include<AssetsLibrary/AssetsLibrary.h>

@interface ViewController : UIViewController{
ALAssetsLibrary *library;

NSArray *imageArray;

NSMutableArray *mutableArray;
}
-(void)allPhotosCollected:(NSArray*)imgArray;
@end


.m

#import "ViewController.h"

@interface ViewController ()

@end

static int count=0;

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
[self getAllPictures];
}

//获取全部图片
-(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


下面是关于AssetsLibrary的一些方法说明,仅供參考

//初始化。这个简单
if (!self.assetsLibrary) {
_assetsLibrary = [[ALAssetsLibrary alloc] init];
}


//加入一个group。成功后查看一些信息
[_assetsLibrary addAssetsGroupAlbumWithName:@"tesddt" resultBlock:^(ALAssetsGroup *group) {
//查看相冊的名字
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
//查看相冊的类型
NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
//查看相冊的存储id
NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
//查看相冊存储的位置地址
NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
} failureBlock:^(NSError *error) {

}];


能够通过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 ] ;


//通过url地址在相冊资源中获取该地址的资源文件ALAsset,有可能是相片或视频
[_library assetForURL:[NSURL URLWithString:@""] resultBlock:^(ALAsset *asset) {


// NSString *const ALAssetPropertyType;
// NSString *const ALAssetPropertyLocation;
// NSString *const ALAssetPropertyDuration;
// NSString *const ALAssetPropertyOrientation;
// NSString *const ALAssetPropertyDate;
// NSString *const ALAssetPropertyRepresentations;
// NSString *const ALAssetPropertyURLs;
// NSString *const ALAssetPropertyAssetURL;



//查看资源的地理位置信息
NSLog(@"ALAssetPropertyLocation:%@",[asset valueForProperty:ALAssetPropertyLocation]);
//假设资源是视频,查看视频的时长
NSLog(@"ALAssetPropertyDuration:%@",[asset valueForProperty:ALAssetPropertyDuration]);
//查看资源的方向,图片的旋转方向
NSLog(@"ALAssetPropertyOrientation:%@",[asset valueForProperty:ALAssetPropertyOrientation]);
//查看资源的创建时间
NSLog(@"ALAssetPropertyDate:%@",[asset valueForProperty:ALAssetPropertyDate]);
//查看资源的描写叙述信息
NSLog(@"ALAssetPropertyRepresentations:%@",[asset valueForProperty:ALAssetPropertyRepresentations]);
NSLog(@"ALAssetPropertyURLs:%@",[asset valueForProperty:ALAssetPropertyURLs]);
//查看资源的url路径
NSLog(@"ALAssetPropertyAssetURL:%@",[asset valueForProperty:ALAssetPropertyAssetURL]);
} failureBlock:^(NSError *error) {

}];


//通过url地址获取相冊资源中的一个相冊
[_library groupForURL:groupURL resultBlock:^(ALAssetsGroup *group) {
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {

}];


//依据选择的类型遍历相冊资源中的相相应类型的全部相冊,当中stop行參是指针。表示是否停止迭代,当赋值为false则停止

// enum {
// ALAssetsGroupLibrary = (1 << 0),
// ALAssetsGroupAlbum = (1 << 1),
// ALAssetsGroupEvent = (1 << 2),
// ALAssetsGroupFaces = (1 << 3),
// ALAssetsGroupSavedPhotos = (1 << 4),
// ALAssetsGroupPhotoStream = (1 << 5),
// ALAssetsGroupAll = 0xFFFFFFFF,
// };

[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSLog(@"group name:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
} failureBlock:^(NSError *error) {

}];


//保存图片到系统默认的相冊中,使用nsdata的形式,并返回照片的url地址
[_library writeImageDataToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

}];


//保存图片到系统默认的相冊中,使用cgimageref的形式。并返回照片的url地址
[_library writeImageToSavedPhotosAlbum:nil metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {

}];


//保存图片到系统默认的相冊中。使用cgimageref的形式,而且选择图片以什么旋转方向的形式保存,并返回照片的url地址

// typedef enum {
// ALAssetOrientationUp, // default orientation
// ALAssetOrientationDown, // 180 deg rotation
// ALAssetOrientationLeft, // 90 deg CCW
// ALAssetOrientationRight, // 90 deg CW
// ALAssetOrientationUpMirrored, // as above but image mirrored along other axis. horizontal flip
// ALAssetOrientationDownMirrored, // horizontal flip
// ALAssetOrientationLeftMirrored, // vertical flip
// ALAssetOrientationRightMirrored, // vertical flip
// } ALAssetOrientation;

UIImage* image = [UIImage imageNamed:@"test.png"];
[_library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationLeft completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(@"save image:%@",assetURL);
}];