ALAssetsGroup类是系统用于映射相册资源中的每个相册,可以通过该类获取相册中的资源文件,并且能向相册中添加资源文件
//保存图片到系统默认的相册中,使用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);
//通过ALAssetsLibrary迭代取出所有相册
[_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
NSString* groupname = [group valueForProperty:ALAssetsGroupPropertyName];
//如果相册的名称是test的时候,对其进行操作
if ([groupname isEqualToString:@"test"]) {
//设置相册组的筛选条件,ALAssetsFilter类表示筛选条件,allPhotos代表相册只包含相片,allVideos代表只包含视频,allAssets代表包含所有资源
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
//通过刚保存的照片的url,把保存到默认相册的照片也保存到test相册中
[_library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
//添加资源到指定的相册
[group addAsset:asset];
//获取相册中一共的资源数量
int count = [group numberOfAssets];
NSLog(@"count:%d",count);
dispatch_queue_t main = dispatch_get_main_queue();
dispatch_async(main, ^{
//获取相册的封面图片
CGImageRef poster = [group posterImage];
[imageView setImage:[UIImage imageWithCGImage:poster]];
});
//NSString *const ALAssetsGroupPropertyName;
//NSString *const ALAssetsGroupPropertyType;
//NSString *const ALAssetsGroupPropertyPersistentID;
//NSString *const ALAssetsGroupPropertyURL;
//查看相册的名字
NSLog(@"ALAssetsGroupPropertyName:%@",[group valueForProperty:ALAssetsGroupPropertyName]);
//查看相册的类型
NSLog(@"ALAssetsGroupPropertyType:%@",[group valueForProperty:ALAssetsGroupPropertyType]);
//查看相册的存储id
NSLog(@"ALAssetsGroupPropertyPersistentID:%@",[group valueForProperty:ALAssetsGroupPropertyPersistentID]);
//查看相册存储的位置地址
NSLog(@"ALAssetsGroupPropertyURL:%@",[group valueForProperty:ALAssetsGroupPropertyURL]);
//按遍历顺序获取指定索引的资源,遍历顺序可以是先序或倒序
/*
enum {
NSEnumerationConcurrent = (1UL << 0),
NSEnumerationReverse = (1UL << 1),
};
typedef NSUInteger NSEnumerationOptions;
*/
[group enumerateAssetsAtIndexes:[NSIndexSet
indexSetWithIndex:0] options:NSEnumerationConcurrent
usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
}];
//按顺便遍历获取相册中所有的资源,index代表资源的索引,stop赋值为false时,会停止遍历
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
}];
//按顺便遍历获取相册中所有的资源,遍历顺序可以是先序或倒序,index代表资源的索引,stop赋值为false时,会停止遍历
[group
enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset
*result, NSUInteger index, BOOL *stop) {
}];
} failureBlock:^(NSError *error) {
}];
}
} failureBlock:^(NSError *error) {
}];
}];