iOS相册多选照片
2014-08-18 10:42
323 查看
我们知道UIKit框架为我们提供了UIImagePickerController 这个控制器来读取相册资源,但是通过这个控制器我们只能选取一张照片,但是如果想选取多张照片,又该如何操作呢?
AssetsLibrary.framework通过这个框架,我们可以读取到相册中所有的照片资源。这个框架主要提供了这么几个类:
ALAssetsLibrary 指的是整个相册库
ALAssetsGroup 指的是相册中的文件夹
ALAsset 指的是文件夹中的照片、视频
以上三个类的使用 :
先通过 ALAssetsLibrary 类创建相册对象,再通过此相册对象循环遍历相册中得文件夹对象:ALAssetsGroup
。再通过每一个文件夹对象,循环遍历此文件夹中的所有的相册、视频对象:ALAsset。此对象中包含了相册、视频数据,通过这样几次遍历,我们就能获取到相册中所有的照片、视频数据。
代码:
OC代码
ALAssetsLibrary *libray = [[ALAssetsLibrary alloc] init];
/*
通过相册库枚举遍历所有的文件夹ALAssetsGroup
usingBlock : 有多少个Group文件夹,则调用多少次block,每次将对应的文件夹Group传过来
*/
[libray enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group != nil) {
//设置过滤对象
// ALAssetsFilter *filter = [ALAssetsFilter allVideos];
// [group setAssetsFilter:filter];
//通过文件夹枚举遍历所有的相片ALAsset对象,有多少照片,则调用多少次block
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result != nil) {
//将result对象存储到数组中
[_data addObject:result];
}
}];
}
//刷新表格,显示照片
[_tableView reloadData];
} failureBlock:^(NSError *error) {
}];
以上代码通过循环遍历获取到所有的相册对象ALAsset,然后存储到数组对象_data中,再通过UITableView显示在单元格中。
以下是单元格的创建,并且取得ALAsset对象中的数据,显示出来:
OC代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identify = @"imageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];
}
ALAsset *asset = [_data objectAtIndex:indexPath.row];
//获取到媒体的类型
NSString *type = [asset valueForProperty:ALAssetPropertyType];
//媒体类型是视频
if ([type isEqualToString:ALAssetTypeVideo]) {
cell.textLabel.text = @"视频";
} else {
cell.textLabel.text = @"照片";
}
//获取到相片、视频的缩略图
CGImageRef cgImage = [asset thumbnail];
UIImage *image = [UIImage imageWithCGImage:cgImage];
cell.imageView.image = image;
return cell;
}
通过这样就能显示所有的相册中得图片
转自:http://www.iphonetrain.com/blog_info/68.html
AssetsLibrary.framework通过这个框架,我们可以读取到相册中所有的照片资源。这个框架主要提供了这么几个类:
ALAssetsLibrary 指的是整个相册库
ALAssetsGroup 指的是相册中的文件夹
ALAsset 指的是文件夹中的照片、视频
以上三个类的使用 :
先通过 ALAssetsLibrary 类创建相册对象,再通过此相册对象循环遍历相册中得文件夹对象:ALAssetsGroup
。再通过每一个文件夹对象,循环遍历此文件夹中的所有的相册、视频对象:ALAsset。此对象中包含了相册、视频数据,通过这样几次遍历,我们就能获取到相册中所有的照片、视频数据。
代码:
OC代码
ALAssetsLibrary *libray = [[ALAssetsLibrary alloc] init];
/*
通过相册库枚举遍历所有的文件夹ALAssetsGroup
usingBlock : 有多少个Group文件夹,则调用多少次block,每次将对应的文件夹Group传过来
*/
[libray enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group != nil) {
//设置过滤对象
// ALAssetsFilter *filter = [ALAssetsFilter allVideos];
// [group setAssetsFilter:filter];
//通过文件夹枚举遍历所有的相片ALAsset对象,有多少照片,则调用多少次block
[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result != nil) {
//将result对象存储到数组中
[_data addObject:result];
}
}];
}
//刷新表格,显示照片
[_tableView reloadData];
} failureBlock:^(NSError *error) {
}];
以上代码通过循环遍历获取到所有的相册对象ALAsset,然后存储到数组对象_data中,再通过UITableView显示在单元格中。
以下是单元格的创建,并且取得ALAsset对象中的数据,显示出来:
OC代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identify = @"imageCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify] autorelease];
}
ALAsset *asset = [_data objectAtIndex:indexPath.row];
//获取到媒体的类型
NSString *type = [asset valueForProperty:ALAssetPropertyType];
//媒体类型是视频
if ([type isEqualToString:ALAssetTypeVideo]) {
cell.textLabel.text = @"视频";
} else {
cell.textLabel.text = @"照片";
}
//获取到相片、视频的缩略图
CGImageRef cgImage = [asset thumbnail];
UIImage *image = [UIImage imageWithCGImage:cgImage];
cell.imageView.image = image;
return cell;
}
通过这样就能显示所有的相册中得图片
转自:http://www.iphonetrain.com/blog_info/68.html
相关文章推荐
- iOS相册多选照片
- ios 使用ELCImagePicker实现相册照片多选和真机拍照调试
- iOS相册多选照片
- iOS 如何正确的从iPhone相册中导出和保存照片?
- iOS 调用系统相册,调用系统照相机,调用系统相册,并且对照片进行剪裁
- IOS开发—自定义多选相册
- iOS 取相册照片/打开相机
- iOS 删除相册中照片--来自简书
- 在iOS App中增加完整的照片多选功能
- iOS拍照后存储照片到相册中
- iOS选择照片、相册问题
- iOS 获取系统相册全部照片以及照片信息
- iOS保存系统相册中的照片到应用沙盒中出现的问题--图片旋转90度,使用CGImageRelease造成应用崩溃
- iOS开发--删除本地相册照片
- Android:仿QQ照片选择器(按相册分类显示,多选添加)
- iOS开发之保存照片到系统相册(Photo Album)
- 在iOS App中增加完整的照片多选功能
- iOS本地照片多选的实现
- iOS中相册-用一个tableView区分照片和video
- IOS多选单选相册图片