ios开发中获取相册中的全部的图片 同时分组呈现出来,具体效果如图所示只是一个简单的例子,具体的功能扩充需要你自己实现
//
// TableViewController.m
// XXBImagePicker
//
// Created by 小小兵 on 15/1/30.
// Copyright (c) 2015年 xiaoxiaobing. All rights reserved.
//
#import "TableViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface TableViewController ()
@property(nonatomic , strong)NSMutableArray *groupArray;
@property(nonatomic , strong)ALAssetsLibrary*library ;
@end
@implementation TableViewController
- (void)viewDidLoad
{
[self getImgs];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groupArray.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"我是第%@组",@(section)];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"我是第%@组",@(section)];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.groupArray.count >0)
{
NSMutableArray *array = self.groupArray[section];
return array.count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
ALAsset *alaset = self.groupArray[indexPath.section][indexPath.row];
//获取到媒体的类型
NSString *type = [alaset valueForProperty:ALAssetPropertyType];
//媒体类型是视频
if ([type isEqualToString:ALAssetTypeVideo])
{
cell.textLabel.text = @"视频";
}
else
{
cell.textLabel.text = @"照片";
}
//获取到相片、视频的缩略图
CGImageRef cgImage = [alaset thumbnail];
UIImage *image = [UIImage imageWithCGImage:cgImage];
cell.imageView.image = image;
return cell;
}
/**
* 获取图片
*/
-(void)getImgs{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
NSLog(@"相册访问失败 =%@", [myerror localizedDescription]);
if ([myerror.localizedDescription rangeOfString:@"Global denied access"].location!=NSNotFound) {
NSLog(@"无法访问相册.请在'设置->定位服务'设置为打开状态.");
}else{
NSLog(@"相册访问失败.");
}
};
ALAssetsGroupEnumerationResultsBlock groupEnumerAtion = ^(ALAsset *result, NSUInteger index, BOOL *stop){
if (result!=NULL)
{
NSLog(@"group %d",self.groupArray.count);
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
{
NSMutableArray *array = [self.groupArray lastObject];
NSLog(@"array count %@",@(array.count));
[array addObject:result];
}
}
/**
* 获取完数据去主线程刷新表格
*/
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
};
/**
* 获取对应的组
*/
ALAssetsLibraryGroupsEnumerationResultsBlock libraryGroupsEnumeration = ^(ALAssetsGroup* group, BOOL* stop){
if (group!=nil)
{
/**
* 有一个新的group的时候就新建一个数组 用来存放这个group里边的多有的资源
*/
NSMutableArray *photoALAssetArray = [NSMutableArray array];
[self.groupArray addObject:photoALAssetArray];
//获取相簿的组
NSLog(@"相册名称 :%@",[group valueForProperty:ALAssetsGroupPropertyName]);
// NSString *name = [group valueForProperty:ALAssetsGroupPropertyName];
// if ([name isEqualToString:@"Camera Roll"])
// {
// name=@"相机胶卷";
// }
// NSLog(@"组名 %@",name);
[group enumerateAssetsUsingBlock:groupEnumerAtion];
}
};
_library = [[ALAssetsLibrary alloc] init];
[_library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:libraryGroupsEnumeration
failureBlock:failureblock];
});
}
- (NSMutableArray *)groupArray
{
if (_groupArray == nil) {
_groupArray = [NSMutableArray array];
}
return _groupArray;
}
#pragma mark - 附加可能需要的知识点
/**
NSString *urlstr=[NSString stringWithFormat:@"%@",result.defaultRepresentation.url];//图片的url
[self.urlArray addObject:urlstr];
*/
//------------------------根据图片的url反取图片-----
//
//ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
//NSURL *url=[NSURL URLWithString:urlStr];
//[assetLibrary assetForURL:url resultBlock:^(ALAsset *asset) {
// UIImage *image=[UIImage imageWithCGImage:asset.thumbnail];
// cellImageView.image=image;
//
//}failureBlock:^(NSError *error) {
// NSLog(@"error=%@",error);
//}
// ];
//---------------------
@end
下边再补充一些知识点,方便各位去扩充功能 保存照片到对应的group中 我这里的group是test
//保存图片到系统默认的相册中,使用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;
*/
- (void)savePhotoToLiboary
{
UIImage* image = [UIImage imageNamed:@"test.png"];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[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];
});
//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) {
}];
}];
}
我在github上有一个demo有兴趣的可以看一下
https://github.com/sixTiger/XXBImagePickerViewController