| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- //
- // 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 ()<UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- {
- 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<NSString *,id> *)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<urls.count; i++) {
- urlStr = [urlStr stringByAppendingFormat:@",%@",urls[i]];
- }
- }
-
- NSDictionary *dic = @{@"modelpk":[ModelUser user].modelpk,@"photo":urlStr};
-
- [YanCNetWorkManager requestPostWithURLStr:Url_addModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
- if ([dataDic[@"msg"] isEqualToString:@"success"]) {
- [MBProgressHUD showSuccess:@"保存成功"];
- [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
- }
- } enError:^(NSError *error) {
-
- }];
- }else{
- [MBProgressHUD showSuccess:@"保存成功"];
- [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
- }
- }
- -(void)deletePhoto:(NSInteger)index{
-
- PhotoModel *model = [self.dataSource objectAtIndex:index];
-
- if (model.localImage) {
- for (UIImage *img in self.updateData ) {
- if (img == model.localImage) {
- [self.updateData removeObject:img];
- }
- }
- }else{
- NSDictionary *dic = @{@"modelpk":[ModelUser user].modelpk,@"modelphotopk":model.modelphotopk};
-
- [YanCNetWorkManager requestPostWithURLStr:Url_deleteModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
- if ([dataDic[@"msg"] isEqualToString:@"success"]) {
-
- }
- } enError:^(NSError *error) {
-
- }];
- }
- [self.dataSource removeObjectAtIndex:index];
- [self.photoListView reloadData];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|