// // ModelPhotoViewController.m // model // // Created by liufei on 2018/7/25. // Copyright © 2018年 Mine. All rights reserved. // #import "ModelPhotoViewController.h" #import "PhotoCollectionViewCell.h" #import "PhotoModel.h" @interface ModelPhotoViewController () { UIImagePickerController *pickerVc ; } @property (nonatomic, strong) UICollectionView *photoListView; @property (nonatomic, strong) NSMutableArray *dataSource; @property (nonatomic, strong) UIButton *saveBtn; @property (nonatomic, strong) NSMutableArray *tempUrl; @property (nonatomic, strong) NSMutableArray *updateData; @property (nonatomic, strong) NSMutableArray *deleteData; @end @implementation ModelPhotoViewController - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (NSMutableArray *)updateData { if (!_updateData) { _updateData = [NSMutableArray array]; } return _updateData; } - (NSMutableArray *)deleteData { if (!_deleteData) { _deleteData = [NSMutableArray array]; } return _deleteData; } - (NSMutableArray *)tempUrl { if (!_tempUrl) { _tempUrl = [NSMutableArray array]; } return _tempUrl; } - (void)viewDidLoad { [super viewDidLoad]; [self configUI]; [self getModelPhoto]; } - (void)getModelPhoto { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [YanCNetWorkManager requestGETWithURLStr:Url_getModelPhoto(PublicUrl) parameters:@{@"modelpk":[ModelUser user].modelpk} finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; if ([dataDic[@"msg"] isEqualToString:@"success"]) { for (NSDictionary *dic in dataDic[@"data"]) { PhotoModel *model = [PhotoModel mj_objectWithKeyValues:dic]; [self.dataSource addObject:model]; } [self.photoListView reloadData]; } } enError:^(NSError *error) { }]; } - (void)configUI { self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); self.title = @"照片"; self.navigationController.navigationBar.tintColor = RGBValueColor(0x333333, 1); //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave)]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20); layout.itemSize = CGSizeMake(ScreenWidth/2 - 25, ScreenWidth/2 - 25); layout.minimumLineSpacing = 10; layout.minimumInteritemSpacing = 10; self.photoListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth) collectionViewLayout:layout]; self.photoListView.delegate = self; self.photoListView.dataSource = self; self.photoListView.backgroundColor = [UIColor whiteColor]; [self.photoListView registerNib:[UINib nibWithNibName:@"PhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])]; [self.view addSubview:self.photoListView]; UILabel *deleteLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.photoListView.frame), 200, 20)]; deleteLabel.text = @"点击可删除照片"; deleteLabel.textColor = RGB(102, 102, 102); deleteLabel.font = [UIFont systemFontOfSize:14]; [self.view addSubview:deleteLabel]; self.saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(20,CGRectGetMaxY(self.photoListView.frame) + 40, ScreenWidth - 40, 48)]; self.saveBtn.layer.cornerRadius = 24.f; self.saveBtn.layer.masksToBounds = YES; [self.saveBtn setTitle:@"保存" forState:UIControlStateNormal]; [self.saveBtn addTarget:self action:@selector(handleSave) forControlEvents:UIControlEventTouchUpInside]; [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"tixian-1"] forState:UIControlStateNormal]; [self.view addSubview:self.saveBtn]; } - (void)backClick { [self.navigationController popViewControllerAnimated:YES]; } #pragma mark -collectionView - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataSource.count+1; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class]) forIndexPath:indexPath]; if (indexPath.item < self.dataSource.count) { PhotoModel *model = self.dataSource[indexPath.item]; if (model.localImage) { cell.userImageView.image = model.localImage; } else { [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", PublicUrl,model.photo]]]; } }else{ cell.userImageView.image = [UIImage imageNamed:@"photo_add"]; } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (self.dataSource.count == indexPath.item) { if (self.dataSource.count >= 9) { [MBProgressHUD showSuccess:@"最多不能超过9张"]; }else{ UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; pickerVc = [[UIImagePickerController alloc] init]; pickerVc.allowsEditing = YES; pickerVc.delegate = self; UIAlertAction *library = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { pickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentViewController:pickerVc animated:YES completion:nil]; }]; UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera; [self presentViewController:pickerVc animated:YES completion:nil]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alertVc addAction:library]; [alertVc addAction:camera]; [alertVc addAction:cancel]; [self presentViewController:alertVc animated:YES completion:nil]; } }else{ UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定删除" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self deletePhoto:indexPath.item]; }]; [alertVc addAction:cancelAction]; [alertVc addAction:sureAction]; [self presentViewController:alertVc animated:YES completion:nil]; } } #pragma mark -imagePickerController - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"]; [picker dismissViewControllerAnimated:NO completion:nil]; PhotoModel *model = [[PhotoModel alloc] init]; model.localImage = image; NSData *photoData = UIImageJPEGRepresentation(image, 0.5); NSInteger length = [photoData length] / 1000; NSLog(@"图片大小:%ld Kb",(long)length); [self.dataSource addObject:model]; [self.updateData addObject:image]; [self.photoListView reloadData]; [self uploadImage:image]; } -(void)uploadImage:(UIImage *)image{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl) fileData:image name:@"img" fileName:@"file.jpg" mimeType:@"image/jpeg" parameters:nil finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; [self.tempUrl addObject:dataDic[@"img"]]; } enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } #pragma mark - - (void)handleSave { [self uploadImageThroughURLs:self.tempUrl]; // [MBProgressHUD showHUDAddedTo:self.view animated:YES]; // dispatch_group_t group = dispatch_group_create(); // // for (NSInteger i = 0; i < self.updateData.count; i++) { // // dispatch_group_enter(group); // // [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl) fileData:self.updateData[i] name:@"img" fileName:@"file.jpg" mimeType:@"image/jpeg" parameters:nil finish:^(id dataDic) { // // [self.tempUrl addObject:dataDic[@"img"]]; // // NSLog(@"第 %d 张图片上传成功: %@", (int)i + 1, dataDic); // dispatch_group_leave(group); // } enError:^(NSError *error) { // NSLog(@"第 %d 张图片上传失败: %@", (int)i + 1, error); // dispatch_group_leave(group); // }]; // } // [MBProgressHUD hideHUDForView:self.view animated:YES]; // dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // NSLog(@"上传完成!"); // // [self uploadImageThroughURLs:self.tempUrl]; // }); } -(void)uploadImageThroughURLs:(NSMutableArray *)urls{ NSString *urlStr = @""; if (urls.count > 0) { urlStr = [urls firstObject]; if (urls.count > 1) { for (int i=1; i