這兩天項(xiàng)目中需要用戶上傳多張圖片昭抒,或者實(shí)時(shí)拍照评也,并多個(gè)照片排列在一排。倒是花費(fèi)了我一番時(shí)間∶鸱担現(xiàn)在寫在這里盗迟。
先上波圖
展示圖.gif
拍照:
Paste_Image.png
在這里要用到github上的一個(gè)開(kāi)源第三方zlphotobrowser,不過(guò)這個(gè)第三方有很多bug熙含,自己拿來(lái)用不是很好使罚缕,需要花時(shí)間改。
由于最近工作比較忙怎静,就不一步步解釋了邮弹,直接將我自己寫的代碼貼一下
在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; //照片數(shù)量
//在取消相冊(cè)之前,記錄下當(dāng)前標(biāo)記過(guò)的照片數(shù)量
NSMutableArray *_biaojiArr;
}
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSMutableArray *assets;
@property (nonatomic, strong) NSMutableArray *arr;//拍照數(shù)組
@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];
//創(chuàng)建刪除按鈕
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) {
//第一個(gè)
NSLog(@"第一個(gè)");
UIImageView *photoView = (UIImageView *)[self.view viewWithTag:index];
ZLPhotoPickerBrowserViewController *browserVc = [[ZLPhotoPickerBrowserViewController alloc] init];
[browserVc showHeadPortrait:photoView];
}
else if (index == 1001) {
NSLog(@"第二個(gè)");
}
if (index == 1100) {
//第一個(gè)差號(hào)
NSLog(@"第一個(gè)差號(hào)");
[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(@"提取相冊(cè)完成,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(@"沒(méi)有攝像頭");
// return;
// }
// //初始化圖片選擇控制器
// UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
// imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;//設(shè)置通過(guò)照相來(lái)選取照片
//
// imagePicker.allowsEditing = YES; //設(shè)置拍照時(shí)的下方的工具欄是否顯示消约,如果需要自定義拍攝界面肠鲫,則可把該工具欄隱藏
// 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(@"當(dāng)前照片已經(jīng)到達(dá)上線");
}
_cameraVc = [[ZLCameraViewController alloc] init];
// 拍照最多個(gè)數(shù)
_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 {
//調(diào)用相冊(cè)或者拍照
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請(qǐng)選擇獲取圖片操作" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *photos = [UIAlertAction actionWithTitle:@"從相冊(cè)中" 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;
}
運(yùn)行后如下:
測(cè)試.gif
如果你喜歡的話或粮,別忘了點(diǎn)贊,如果幫到了你的話捞高,別忘了請(qǐng)我吃辣條如果你有不懂氯材,可以留言,我有時(shí)間會(huì)回復(fù)的為了更美好的明天硝岗,睡覺(jué)氢哮。