php手机i选择图片多选,自定义照片选择器(可多选)

自定义多选照片选择器

一.核心类

自定义照片选择器的核心类是ALAssetsLibrary ALAssetsGroup 和ALAsset三个类

二. 类介绍

ALAssetsLibrary是核心类, 通过ALAssetsLibrary对象可以实现查看相册列表,增加相册,保存图片到相册等功能。

例如enumerateGroupsWithTypes方法列举所有相册,获得所有所有相簿信息(即: ALAssetsGroup对象)

ALAssetsGroup类是相册类, 通过ALAssetsGroup可以获得该相册的所有信息, 可以通过valueForProperty方法查看不同属性的值,如:ALAssetsGroupPropertyName,相册名。如:

(CGImageRef)posterImage 相册封面的缩略图

(NSInteger)numberOfAssets该相册中的相片数.

enumerateObjectsUsingBlock遍历可获得相册中的所有相片对象(即:ALAsset对象).

setAssetsFilter:(ALAssetsFilter *)filter过滤照片或者视频等。

ALAsset是照片的类, ALAsset类有一个defaultRepresentation方法,返回值是ALAssetRepresentation类,该类的作用就是获取该资源图片的详细资源信息。 它提供了对应的方法来获取该照片的所有信息,如照片\尺寸\照片位置\拍摄日期等

三. 思路

* 相册选择界面: UITableView

* 查看相册照片: UICollectionView

modal相册界面的时候, 通过ALAssetsLibrary对象的enumerateGroupsWithTypes方法, 遍历获得所有相册的ALAssetsGroup对象

用可变数组保存ALAssetsGroup对象, 实现tableView的数据源方法

点击tableView的cell时将对于的ALAssetsGroup对象传递给 collectionView

ALAssetsGroup遍历获得该相册中的所有ALAsset对象. ALAsset类有一个defaultRepresentation方法,返回值是ALAssetRepresentation类,该类的作用就是获取该资源图片的详细资源信息。通过ALAsset提供的方法获得照片的thumbImg缩略图等信息, 实现collectionView数据源方法

四. 核心代码

1.获得相册信息

- (void)getAssetsGroupData {

// 因为是异步的, 所以使用operation依赖

__weak typeof(self) weakSelf = self;

NSOperationQueue *queue = [NSOperationQueue mainQueue];

NSBlockOperation *enumOp = [NSBlockOperation blockOperationWithBlock:^{

// - enumerateGroupsWithTypes:方法遍历获得相簿ALAssetsGroup对象

[self.assetslibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

if (group) {

[weakSelf.albumArrayM insertObject:group atIndex:0];

NSLog(@"%@", [group valueForProperty:ALAssetsGroupPropertyName]);

}

} failureBlock:^(NSError *error) {

NSLog(@"获取相簿失败%@", error);

}];

}];

NSBlockOperation *reloadOp = [NSBlockOperation blockOperationWithBlock:^{

[weakSelf.tableView reloadData];

}];

[reloadOp addDependency:enumOp];

[queue addOperation:enumOp];

[queue addOperation:reloadOp];

}

#pragma mark - lazy loading

- (ALAssetsLibrary *)assetslibrary {

if (!_assetslibrary) {

_assetslibrary = [[ALAssetsLibrary alloc] init];

}

return _assetslibrary;

}

2.获取照片YSAsset信息

- (void)handleData:(NSArray *)assetArray {

__weak typeof(self) weakSelf = self;

[assetArray enumerateObjectsUsingBlock:^(YSAsset *obj, NSUInteger idx, BOOL * _Nonnull stop) {

NSUInteger index = [weakSelf.assetArray indexOfObject:obj];

YSAsset *asset = weakSelf.assetArray[index];

asset.isSelected = obj.isSelected;

// 如果obj不在selectedAssetArrayM数组中, 同时obj是选中的状态, 则添加到selectedAssetArrayM中

// 如果obj在selectedAssetArrayM数组中, 但是obj是不是选中的状态, 则将obj从selectedAssetArrayM中移除

if ([weakSelf.selectedAssetArrayM indexOfObject:obj] == NSNotFound && obj.isSelected) {

[weakSelf.selectedAssetArrayM addObject:asset];

} else if ([weakSelf.selectedAssetArrayM indexOfObject:obj] != NSNotFound && !obj.isSelected) {

[weakSelf.selectedAssetArrayM removeObject:asset];

}

}];

[weakSelf.photosCollectionView reloadData];

[weakSelf updateActionToolbarInfo];

}

- (void)setGroup:(ALAssetsGroup *)group {

__block NSMutableArray *assetArrayM = [NSMutableArray array];

[group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) { // 遍历获取缩略图

if (result) {

YSAsset *assert = [YSAsset assetWithAsset:result]; // 将 ALAsset 转换成 YSAsset

[assetArrayM insertObject:assert atIndex:0];

}

}];

self.assetArray = assetArrayM.copy;

}

3.获得照片信息

> ALAsset类有一个defaultRepresentation方法,返回值是ALAssetRepresentation类,该类的作用就是获取该资源图片的详细资源信息。

ALAssetRepresentation *assetRepresentation = [assert.asset defaultRepresentation];

_kImageView.image = [UIImage imageWithCGImage:[assetRepresentation fullResolutionImage]];

五. ALAssetRepresentation主要方法:

//获取资源图片的详细资源信息

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地址是一样的

NSURL* url = [representation url];

NSLog(@"url:%@",url);

//资源图片uti,唯一标示符

NSLog(@"uti:%@",[representation UTI]);

六:代码:

七:感谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值