// // PhotosUploadViewController.m // LN_School // // Created by 张嵘 on 2018/10/10. // Copyright © 2018 Danson. All rights reserved. // #import "PhotosUploadViewController.h" #import "PureCamera.h" #import "PhotosUploadCell.h" typedef void (^UploadImageBlock) (NSArray *imgPathArr); typedef NS_ENUM(NSInteger, EditImgType) { EditImgType_Add = 0, EditImgType_Replace, }; @interface PhotosUploadViewController () @property (nonatomic, strong) ReturnImagesBlock returnImagesBlock; @property (nonatomic, strong) NSMutableArray *imagesArr; @property (nonatomic, strong) NSMutableArray *imagesPathArr; @property (nonatomic, strong) UILabel *pictureNumberLabel; @property (nonatomic, assign) CGFloat itemWith; @property (nonatomic, assign) EditImgType editImgType; @property (nonatomic, assign) NSInteger currentTag; @end static NSString *collectionIdentifer = @"PhotosUploadCell"; @implementation PhotosUploadViewController - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil imagesArr:(NSArray *)imagesArr imagesPathArr:(NSArray *)imagesPathArr returnImagesBlock:(ReturnImagesBlock)returnImagesBlock { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { _returnImagesBlock = returnImagesBlock; if (imagesArr) { self.imagesArr = [NSMutableArray arrayWithArray:imagesArr]; }else { self.imagesArr = [NSMutableArray array]; } if (imagesPathArr) { self.imagesPathArr = [NSMutableArray arrayWithArray:imagesPathArr]; }else { self.imagesPathArr = [NSMutableArray array]; } } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.title = @"图片上传"; self.view.backgroundColor = KBackGroundColor; [self goBackByNavigation]; [self initCollectView]; [self.view addSubview:self.pictureNumberLabel]; [self reactiveObjCBind]; } //返回按钮点击事件 - (void)goBackByNav { if (_imagesArr.count > 0) { [RQ_SHARE_FUNCTION showAlertWithTitle:@"是否保存当前图片" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) { if (selectedOtherButtonIndex == 0) { if (_returnImagesBlock) { _returnImagesBlock(_imagesArr, self.imagesPathArr); } [self.view endEditing:1]; [self.navigationController popViewControllerAnimated:YES]; }else if (selectedOtherButtonIndex == NSNotFound) { [self.view endEditing:1]; [self.navigationController popViewControllerAnimated:YES]; } }]; }else { [self.view endEditing:1]; [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark - UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.imagesArr.count + 1; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PhotosUploadCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionIdentifer forIndexPath:indexPath]; if (cell==nil) { cell = [[[NSBundle mainBundle] loadNibNamed:@"PhotosUploadCell" owner:nil options:nil] lastObject]; } cell.deleteBtn.hidden = YES; if (indexPath.row == self.imagesArr.count) { [cell.bgImg setImage:[UIImage imageNamed:@"AddImage"]]; }else{ // [cell.bgImg sd_setImageWithURL:[NSURL URLWithString:self.imagesArr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"AddImage"]]; cell.bgImg.image = self.imagesArr[indexPath.row]; cell.deleteBtn.tag = 1000 + indexPath.row; cell.deleteBtn.hidden = NO; [cell.deleteBtn addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchUpInside]; } return cell; } #pragma mark - UICollectionViewDelegate - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == self.imagesArr.count) { //添加 if (self.imagesArr.count >= 6) { ShowMsg(@"最多选择6张图片"); return; } [self takePhotoWithTag:indexPath.row editImgType:EditImgType_Add]; }else{ //替换 [self takePhotoWithTag:indexPath.row editImgType:EditImgType_Replace]; } } #pragma mark - Custom Way //绑定监听数组内容信号 - (void)reactiveObjCBind { [RACObserve(self, imagesArr) subscribeNext:^(id _Nullable x) { NSArray *arr = x; _pictureNumberLabel.text = [NSString stringWithFormat:@"添加图片%lu/6 ",(unsigned long)arr.count]; [self.collectView reloadData]; }]; } //保存按钮点击事件 - (IBAction)saveBtnAction:(id)sender { if (_imagesArr.count > 0) { [RQ_SHARE_FUNCTION showAlertWithTitle:@"保存图片并返回上一页" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) { if (selectedOtherButtonIndex == 0) { if (_returnImagesBlock) { _returnImagesBlock(_imagesArr, self.imagesPathArr); } [self.view endEditing:1]; [self.navigationController popViewControllerAnimated:YES]; } }]; }else { ShowMsg(@"请上传图片"); } } //移除图片 - (void)removeAction:(UIButton*)sender { [RQ_SHARE_FUNCTION showAlertWithTitle:@"是否删除这张图片" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) { if (selectedOtherButtonIndex == 0) { [[self mutableArrayValueForKey:@"imagesArr"] removeObjectAtIndex:sender.tag - 1000]; [[self mutableArrayValueForKey:@"imagesPathArr"] removeObjectAtIndex:sender.tag - 1000]; } }]; } //collecViews设置 - (void)initCollectView { _itemWith = kScreenWidth / 4; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.itemSize = CGSizeMake(_itemWith - 10, _itemWith - 10); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 0; layout.sectionInset = UIEdgeInsetsMake(23, 10, 0, 10);//上左下右 [self.collectView setCollectionViewLayout:layout]; [self.collectView registerNib:[UINib nibWithNibName:collectionIdentifer bundle:nil] forCellWithReuseIdentifier:collectionIdentifer]; _collectView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.00]; } - (void)takePhotoWithTag:(NSInteger)tag editImgType:(EditImgType)editImgType { //100*120 self.currentTag = tag; self.editImgType = editImgType; [RQ_SHARE_FUNCTION showAlertWithTitle:@"请选择获取图片方式" message:nil alertControllerStyle:UIAlertControllerStyleActionSheet cancelButtonTitle:@"取消" otherButtonTitles:@[@"拍照", @"本地照片"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) { if (selectedOtherButtonIndex == 0) { [RQ_SHARE_FUNCTION getPhotosWithGetPhotosWay:GetPhotosWay_Camera size:CGSizeZero maxLength:100 maxImagesCount:1 photosBlock:^(NSArray * _Nonnull imagesArr, NSArray * _Nonnull imagesDataStrArr) { [self uploadHeadImg:imagesDataStrArr completeBlock:^(NSArray *imgPathArr) { if (editImgType == EditImgType_Add) { for (UIImage *image in imagesArr) { [[self mutableArrayValueForKey:@"imagesArr"] addObject:image]; } for (NSString *imgPath in imgPathArr) { [[self mutableArrayValueForKey:@"imagesPathArr"] addObject:imgPath]; } }else if (editImgType == EditImgType_Replace && imgPathArr.count > 0) { [[self mutableArrayValueForKey:@"imagesArr"] replaceObjectAtIndex:tag withObject:imagesArr[0]]; [[self mutableArrayValueForKey:@"imagesPathArr"] replaceObjectAtIndex:tag withObject:imgPathArr[0]]; } }]; }]; }else if (selectedOtherButtonIndex == 1) { [RQ_SHARE_FUNCTION getPhotosWithGetPhotosWay:GetPhotosWay_Album size:CGSizeZero maxLength:100 maxImagesCount:editImgType == EditImgType_Replace? 1 : 6 photosBlock:^(NSArray * _Nonnull imagesArr, NSArray * _Nonnull imagesDataStrArr) { [self uploadHeadImg:imagesDataStrArr completeBlock:^(NSArray *imgPathArr) { if (editImgType == EditImgType_Add) { for (UIImage *image in imagesArr) { [[self mutableArrayValueForKey:@"imagesArr"] addObject:image]; } for (NSString *imgPath in imgPathArr) { [[self mutableArrayValueForKey:@"imagesPathArr"] addObject:imgPath]; } }else if (editImgType == EditImgType_Replace && imgPathArr.count > 0) { [[self mutableArrayValueForKey:@"imagesArr"] replaceObjectAtIndex:tag withObject:imagesArr[0]]; [[self mutableArrayValueForKey:@"imagesPathArr"] replaceObjectAtIndex:tag withObject:imgPathArr[0]]; } }]; }]; } }]; } #pragma mark - Request Data - (void)uploadHeadImg:(NSArray *)imageArr completeBlock:(UploadImageBlock)uploadImageBlock { if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } //@RQ-MARK 1.0.2修改:“学员报名”模块新增 协议上传(上传多张) NSMutableDictionary *dic = [NSMutableDictionary dictionary]; [dic setObject:imageArr.count > 1? [imageArr componentsJoinedByString:@","] : imageArr[0] forKey:@"content"]; NSString *method = @"uploadStuProtocol"; [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) { if (!root) { [self showUPloadImgAlertWithImg:imageArr]; return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"msg"]); [self showUPloadImgAlertWithImg:imageArr]; return; } //上传成功 if (uploadImageBlock && root[@"body"]) { if ([root[@"body"] containsString:@","]) { uploadImageBlock([root[@"body"] componentsSeparatedByString:@","]); }else { uploadImageBlock(@[root[@"body"]]); } } }]; } - (void)showUPloadImgAlertWithImg:(NSArray *)imageArr { __block NSArray *arr = imageArr; [RQ_SHARE_FUNCTION showAlertWithTitle:@"图片上传失败!" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"再试一次"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) { if (selectedOtherButtonIndex == 0) { [self uploadHeadImg:arr completeBlock:^(NSArray *imgPathArr) { if (self.editImgType == EditImgType_Add) { for (UIImage *image in arr) { [[self mutableArrayValueForKey:@"imagesArr"] addObject:image]; } for (NSString *imgPath in imgPathArr) { [[self mutableArrayValueForKey:@"imagesPathArr"] addObject:imgPath]; } }else if (self.editImgType == EditImgType_Replace && imgPathArr.count > 0) { [[self mutableArrayValueForKey:@"imagesArr"] replaceObjectAtIndex:self.currentTag withObject:arr[0]]; [[self mutableArrayValueForKey:@"imagesPathArr"] replaceObjectAtIndex:self.currentTag withObject:imgPathArr[0]]; } }]; } }]; } #pragma mark - Lazy Loading - (UILabel *)pictureNumberLabel { if (!_pictureNumberLabel) { _pictureNumberLabel = [[UILabel alloc]init]; _pictureNumberLabel.backgroundColor = [UIColor clearColor]; _pictureNumberLabel.textAlignment = NSTextAlignmentLeft; _pictureNumberLabel.font = [UIFont boldSystemFontOfSize:12]; _pictureNumberLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0]; _pictureNumberLabel.backgroundColor = [UIColor whiteColor]; _pictureNumberLabel.frame = CGRectMake(10, 3, kScreenWidth - 10, 15); } return _pictureNumberLabel; } @end