iOS中从相册选取多个照片或拍照,可滑动
2016-10-25 本文已影响5740人
哈哈大p孩
这两天项目中需要用户上传多张图片,或者实时拍照,并多个照片排列在一排。倒是花费了我一番时间。现在写在这里。
先上波图
展示图.gif
拍照:
Paste_Image.png
在这里要用到github上的一个开源第三方zlphotobrowser,不过这个第三方有很多bug,自己拿来用不是很好使,需要花时间改。
由于最近工作比较忙,就不一步步解释了,直接将我自己写的代码贴一下
在ViewController.m中
#import "ViewController.h"
#import "UIImage+ZLPhotoLib.h"
#import "ZLPhoto.h"
#import "OneTableViewCell.h"
//#import "UIButton+WebCache.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource,ZLPhotoPickerBrowserViewControllerDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
int MaxPhotos; //照片数量
//在取消相册之前,记录下当前标记过的照片数量
NSMutableArray *_biaojiArr;
}
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSMutableArray *assets;
@property (nonatomic, strong) NSMutableArray *arr;//拍照数组
@property (nonatomic, strong) ZLCameraViewController *cameraVc;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
MaxPhotos = 5;
_biaojiArr = [[NSMutableArray alloc] init];
[self subTableView];
}
- (void)subTableView {
_tableView = [[UITableView alloc ] initWithFrame:self.view.frame];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (NSMutableArray *)assets{
if (!_assets) {
_assets = [NSMutableArray array];
}
return _assets;
}
- (NSMutableArray *)arr {
if (!_arr) {
_arr = [NSMutableArray array];
}
return _arr;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = @"jiege";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
if (indexPath.row == 2) {
OneTableViewCell *cell = [[OneTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"one"];
cell.selectBtn.backgroundColor = [UIColor blueColor];
[cell.selectBtn addTarget:self action:@selector(diaoyao) forControlEvents:UIControlEventTouchUpInside];
cell.collectionView.showsHorizontalScrollIndicator = NO;
cell.collectionView.delegate = self;
cell.collectionView.dataSource = self;
return cell;
}
return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.assets.count;
}
- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath{
return CGSizeMake ( 60 , 30 );
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 15;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
UIImageView *photoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
photoView.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:photoView];
photoView.tag = indexPath.row+1000;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapp:)];
photoView.userInteractionEnabled = YES;
[photoView addGestureRecognizer:tap];
//创建删除按钮
UIImageView *deleteView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
deleteView.backgroundColor = [UIColor redColor ];
[photoView addSubview:deleteView];
deleteView.tag = indexPath.row + 1100;
UITapGestureRecognizer *detap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapp:)];
deleteView.userInteractionEnabled = YES;
[deleteView addGestureRecognizer:detap];
NSLog(@"5555555=assets = %ld", self.assets.count);
ZLPhotoAssets *asset = self.assets[indexPath.row];
// ZLPhotoAssets *asset = _biaojiArr[indexPath.row];
if ([asset isKindOfClass:[ZLPhotoAssets class]]) {
photoView.image = asset.originImage;
}else if ([asset isKindOfClass:[UIImage class]]) {
photoView.image = (UIImage *)asset;
}else if ([asset isKindOfClass:[ZLCamera class]]) {
photoView.image = [(ZLCamera *)asset photoImage];
}
deleteView.backgroundColor = [UIColor redColor];
return cell;
}
- (void)tapp:(UIGestureRecognizer *)tap {
int index = (int)[tap view].tag;
if (index == 1000) {
//第一个
NSLog(@"第一个");
UIImageView *photoView = (UIImageView *)[self.view viewWithTag:index];
ZLPhotoPickerBrowserViewController *browserVc = [[ZLPhotoPickerBrowserViewController alloc] init];
[browserVc showHeadPortrait:photoView];
}
else if (index == 1001) {
NSLog(@"第二个");
}
if (index == 1100) {
//第一个差号
NSLog(@"第一个差号");
[self.assets removeObjectAtIndex:index % 1100];
[_tableView reloadData];
}
}
- (void)handleBtn {
// ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
// // MaxCount, Default = 9
// pickerVc.maxCount = 9;
// // Jump AssetsVc
// pickerVc.status = PickerViewShowStatusCameraRoll;
// // Filter: PickerPhotoStatusAllVideoAndPhotos, PickerPhotoStatusVideos, PickerPhotoStatusPhotos.
// pickerVc.photoStatus = PickerPhotoStatusPhotos;
// // Recoder Select Assets
// pickerVc.selectPickers = self.assets;
// // Desc Show Photos, And Suppor Camera
// pickerVc.topShowPhotoPicker = YES;
// pickerVc.isShowCamera = YES;
// // CallBack
// pickerVc.callBack = ^(NSArray<ZLPhotoAssets *> *status){
// self.assets = status.mutableCopy;
// // [self reloadScrollView];
// NSLog(@"提取相册完成,assets == %ld", self.assets.count);
// [_tableView reloadData];
// };
// [pickerVc showPickerVc:self];
NSLog(@"66666=assets = %ld", self.assets.count);
// _biaojiArr = [NSMutableArray arrayWithArray:self.assets];
_biaojiArr = [self.assets mutableCopy];
NSLog(@"_biaoji == %ld", _biaojiArr.count);
ZLPhotoPickerViewController *pickerVc = [[ZLPhotoPickerViewController alloc] init];
NSArray *array = [[NSArray alloc] initWithArray:self.assets];
pickerVc.selectPickers = array;
pickerVc.maxCount = MaxPhotos;
for (ZLPhotoAssets *photo in array) {
if ([photo isKindOfClass:[ZLCamera class]]){
pickerVc.maxCount -= 1;
// NSLog(@"333333=assets = %ld", self.assets.count);
} else if ([photo isKindOfClass:[ZLPhotoAssets class]]) {
[self.assets removeObject:photo];
// NSLog(@"22222=assets = %ld", self.assets.count);
}
// NSLog(@"1111111=assets = %ld", self.assets.count);
}
if (self.assets.count != 0) {
pickerVc.maxCount = MaxPhotos - self.assets.count;
}
pickerVc.status = PickerViewShowStatusCameraRoll;
// NSLog(@"444444=assets = %ld", self.assets.count);
[pickerVc showPickerVc:self];
__weak typeof(self) weakSelf = self;
pickerVc.callBack = ^(NSArray *assets){
// [weakSelf.assert removeAllObjects];
[weakSelf.assets addObjectsFromArray:assets];
[weakSelf.tableView reloadData];
};
//接受取消观察者信息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"tongzhi" object:nil];
}
-(void)notice:(id)sender{
NSLog(@"%@",sender);
_assets = [_biaojiArr mutableCopy];
NSLog(@"_assssss == %ld", _assets.count);
}
- (void)paizhao {
// //判断摄像头是否可用
// BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
//
// if (!isCamera) {
// NSLog(@"没有摄像头");
// return;
// }
// //初始化图片选择控制器
// UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//设置通过照相来选取照片
//
// imagePicker.allowsEditing = YES; //设置拍照时的下方的工具栏是否显示,如果需要自定义拍摄界面,则可把该工具栏隐藏
// imagePicker.delegate = self;
// [self presentViewController:imagePicker animated:YES completion:nil];
NSLog(@"camera count == %ld", _cameraVc.maxCount);
NSLog(@"asset == %ld", _assets.count);
if (_assets.count >= MaxPhotos) {
NSLog(@"当前照片已经到达上线");
}
_cameraVc = [[ZLCameraViewController alloc] init];
// 拍照最多个数
_cameraVc.maxCount = MaxPhotos-self.assets.count;
__weak typeof(self) weakSelf = self;
_cameraVc.callback = ^(NSArray *cameras){
for (id object in cameras) {
[weakSelf.assets addObject:object];
}
// [weakSelf.assets addObjectsFromArray:cameras];
[weakSelf.tableView reloadData];
};
[_cameraVc showPickerVc:self];
}
- (void)diaoyao {
//调用相册或者拍照
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择获取图片操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *photos = [UIAlertAction actionWithTitle:@"从相册中" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self handleBtn];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
UIAlertAction *makePhotos = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self paizhao];
}];
[alert addAction:photos];
[alert addAction:makePhotos];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:^{
}];
}
里面用到的collectionView,是我们自定义cell里面的
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
_collectionView = [[ UICollectionView alloc ] initWithFrame:CGRectMake(80, 10, self.frame.size.width - 80 , 30) collectionViewLayout :layout];
_collectionView.backgroundColor = [UIColor whiteColor];
[_collectionView registerClass :[UICollectionViewCell class ] forCellWithReuseIdentifier : cellId ];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
[self.contentView addSubview:_collectionView];
}
return _collectionView;
}
运行后如下:
测试.gif
如果你喜欢的话,别忘了点赞,如果帮到了你的话,别忘了请我吃辣条如果你有不懂,可以留言,我有时间会回复的为了更美好的明天,睡觉。