首页 > 代码库 > iOS开发之获取系统相册ALAssetLibrary
iOS开发之获取系统相册ALAssetLibrary
ALAssetsLibrary包含,ALAssetsLibrary,ALAssetsGroup,ALAsset,ALAssetsFilter,ALAssetRepresentation几个类
1、框架解析
(1) ALAssetsLibrary
ALAssetsLibrary类可以实现查看相册列表,增加相册,保存图片到相册等功能
具体实现见框架使用
(2) ALAssetsGroup
ALAssetsGroup就是相册的类,可以通过ALAssetsLibrary遍历得到相册ALAssetsGroup
通过valueForProperty:获取相册名称
[assetsGroup valueForProperty:ALAssetsGroupPropertyName]
ALAssetsGroup 可以使用setAssetsFilter:(ALAssetsFilter *)filter过滤照片或者视频详情见ALAssetsFilter
(3) ALAsset
ALAsset就是一个资源文件,一个ALAssetsGroup下会有多个资源文件,获取相册和视频就是通过ALAssetsLibrary遍历得到相册ALAssetsGroup,
然后通过ALAssetsGroup遍历得到资源文件ALAsset
通过valueForProperty:获取资源文件的信息:
//查看资源的文件类型
[asset valueForProperty:ALAssetPropertyType]
//查看资源的文件类型
[asset valueForProperty:ALAssetPropertyType]
//查看资源的地理位置信息
[asset valueForProperty:ALAssetPropertyLocation]
//如果资源是视频,查看视频的时长
[asset valueForProperty:ALAssetPropertyDuration]
//查看资源的方向,图片的旋转方向
[asset valueForProperty:ALAssetPropertyOrientation]
//查看资源的创建时间
[asset valueForProperty:ALAssetPropertyDate]
//查看资源的描述信息
[asset valueForProperty:ALAssetPropertyRepresentations]
//查看资源的url路径
[asset valueForProperty:ALAssetPropertyAssetURL]
通过 asset.defaultRepresentation 可以获取资源文件的url、size、name等信息
// 获取资源文件url
asset.defaultRepresentation.url
// 获取资源文件大小
asset.defaultRepresentation.size
// 获取资源文件名称
asset.defaultRepresentation.filename
(4) ALAssetsFilter
ALAssetsFilter主要用于过滤图片和视频
通过setAssetsFilter:筛选资源文件
//筛选图片和视频
[group setAssetsFilter:[ALAssetsFilter allAssets]];
//筛选图片
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
//筛选视频
[group setAssetsFilter:[ALAssetsFilter allVideos]];
(5) ALAssetRepresentation
ALAsset类有一个defaultRepresentation方法,返回值是ALAssetRepresentation类,该类的作用就是获取该资源图片的详细资源信息。
通过representation 获取资源文件信息
// //获取资源图片的详细资源信息
ALAssetRepresentation* representation = [asset defaultRepresentation];
//获取资源图片的长宽
CGSize dimension = [representation dimensions];
//获取资源图片的高清图
[representation fullResolutionImage];
//获取资源图片的全屏图
[representation fullScreenImage];
//获取资源图片的名字
NSString* filename = [representation filename]; NSLog(@"filename:%@",filename);
//缩放倍数
[representation scale];
//图片资源容量大小
[representation size];
//图片资源原数据
[representation metadata];
//旋转方向
[representation orientation];
//资源图片url地址,该地址和ALAsset通过ALAssetPropertyAssetURL获取的url地址是一样的
[representation url];
//资源图片uti,唯一标示符
[representation UTI]
2、框架使用
(1)遍历得到图片视频资源
(2)在系统相册中创建相册
- (void)createAlbumInPhone
{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
NSMutableArray *groups=[[NSMutableArray alloc]init];
ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[groups addObject:group];
} else {
BOOL haveHDRGroup = NO;
for (ALAssetsGroup *gp in groups) {
NSString *name =[gp valueForProperty:ALAssetsGroupPropertyName];
if ([name isEqualToString:@"联系家庭云"]) {
haveHDRGroup = YES;
}
}
if (!haveHDRGroup) {
[assetsLibrary addAssetsGroupAlbumWithName:@"联系家庭云" resultBlock:^(ALAssetsGroup *group) {
[groups addObject:group];
} failureBlock:nil];
haveHDRGroup = YES;
}
}
};
//创建相簿
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAlbum usingBlock:listGroupBlock failureBlock:nil];
}
(3)在系统相册中保存图片
- (void)saveImageToAlbum:(UIImage *)image
{
[self saveToAlbumWithMetadata:nil imageData:UIImageJPEGRepresentation(image,1.0) customAlbumName:@"联系家庭云" completionBlock:^{
// 创建成功执行成功代码
} failureBlock:^(NSError *error) {
//处理添加失败的方法显示alert让它回到主线程执行,不然那个框框死活不肯弹出来
dispatch_async(dispatch_get_main_queue(), ^{
//添加失败一般是由用户不允许应用访问相册造成的,这边可以取出这种情况加以判断一下
if([error.localizedDescription rangeOfString:@"User denied access"].location != NSNotFound ||[error.localizedDescription rangeOfString:@"用户拒绝访问"].location!=NSNotFound){
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:error.localizedDescription message:error.localizedFailureReason delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles: nil];
[alert show];
}
});
}];
}
- (void)saveToAlbumWithMetadata:(NSDictionary *)metadata imageData:(NSData *)imageData customAlbumName:(NSString *)customAlbumName completionBlock:(void (^)(void))completionBlock failureBlock:(void (^)(NSError *error))failureBlock
{
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
void (^AddAsset)(ALAssetsLibrary *, NSURL *) = ^(ALAssetsLibrary *assetsLibrary, NSURL *assetURL) {
[assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if ([[group valueForProperty:ALAssetsGroupPropertyName] isEqualToString:customAlbumName]) {
[group addAsset:asset];
if (completionBlock) {
completionBlock();
}
}
} failureBlock:^(NSError *error) {
if (failureBlock) {
failureBlock(error);
}
}];
} failureBlock:^(NSError *error) {
if (failureBlock) {
failureBlock(error);
}
}];
};
[assetsLibrary writeImageDataToSavedPhotosAlbum:imageData metadata:metadata completionBlock:^(NSURL *assetURL, NSError *error) {
if (customAlbumName) {
[assetsLibrary addAssetsGroupAlbumWithName:customAlbumName resultBlock:^(ALAssetsGroup *group) {
if (group) {
[assetsLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
[group addAsset:asset];
if (completionBlock) {
completionBlock();
}
} failureBlock:^(NSError *error) {
if (failureBlock) {
failureBlock(error);
}
}];
} else {
AddAsset(assetsLibrary, assetURL);
}
} failureBlock:^(NSError *error) {
AddAsset(assetsLibrary, assetURL);
}];
} else {
if (completionBlock) {
completionBlock();
}
}
}];
}
iOS开发之获取系统相册ALAssetLibrary