需要选择单张图片,可以直接调用imagePickerViewController
但是往往我们需要同时上传很多张
我们公司的系统要求是iOS8.0以上都支持,所以就基于AssetsLibrary自己做了一个
先气看代码麻烦可以直接看我GitHub上的demo,编码辛苦,觉得有用还望给个star以作鼓励
https://github.com/gofey/PhoneAlum
效果如下
必须导入
#import
GFPhotoAlumController.m文件
先遍历所有相册分组
//self.assetLib是一个可变数组,将照片数大于0的ALAssetsGroup放进去
[self.assetLib enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
if (group.numberOfAssets > 0) {
[self.assetGroupArray addObject:group];
}
}else{
if (self.assetGroupArray.count > 0) {
[self.tableView reloadData];
}else{
//no photo
}
}
} failureBlock:^(NSError *error) {
NSLog(@"enumerateGroupsError:%@",error);
}];
在tableView的cell中显示
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:_reuseIdentifier];
ALAssetsGroup *group = self.assetGroupArray[indexPath.row];
cell.imageView.clipsToBounds = YES;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.image = [UIImage imageWithCGImage:group.posterImage];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", [group valueForProperty:ALAssetsGroupPropertyName],[NSString stringWithFormat:@"(%ld)", (long)group.numberOfAssets]];
return cell;
GFPhotosController.m文件
由于系统ALAsset是没有这个照片是否被选中状态的,所以需要自己做一个
如下
@interface GFAsset : NSObject
@property(nonatomic)BOOL isSelected;
@property(nonatomic,strong)ALAsset *asset;
- (GFAsset *)initWithAsset:(ALAsset *)asset;
+ (GFAsset *)assetWithAsset:(ALAsset *)asset;
@end
选择的group
遍历Group,获取GFAsset
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
if (result) {
[self.assetArray addObject:[GFAsset assetWithAsset:result]];
}else{
if (self.assetArray.count == 0) {
NSLog(@" no photo");
}
[self.collectionView reloadData];
}
}];
将照片付给imageView
self.photoImgView.image = [UIImage imageWithCGImage:asset.asset.aspectRatioThumbnail];
当然这只是展示照片的主要代码,详细逻辑和内部实现,以及其他操作,请参考https://github.com/gofey/PhoneAlum