// // SelectImgVC.m // jiaPeiC // // Created by apple on 16/6/30. // Copyright © 2016年 JCZ. All rights reserved. // #import "SelectImgVC.h" #import "SelectCell.h" #import "HolderView.h" #import "STTButton.h" #import "MJPhotoBrowser.h" #import "MJPhoto.h" @interface SelectImgVC () { HolderView *holderV; UICollectionView *mainCollection; UIBarButtonItem *item; UIView *holderBar; UILabel *countLabel; NSMutableArray *dataArray; NSMutableArray *delArray; BOOL isEdit; } @end @implementation SelectImgVC - (void)viewDidLoad { [super viewDidLoad]; self.title = [NSString stringWithFormat:@"%@的照片",_stuDic[@"STU_NAME"]? _stuDic[@"STU_NAME"]:_stuDic[@"STUNAME"]]; self.view.backgroundColor = backGroundColor; dataArray = [NSMutableArray array]; delArray = [NSMutableArray array]; // item = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(clickToDelete)]; // [item setTintColor:defGreen]; // [self.navigationItem setRightBarButtonItem:item]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; CGFloat width = (kSize.width - 30)/3.0; [layout setItemSize:CGSizeMake(width, width + 60)]; [layout setSectionInset:UIEdgeInsetsMake(10, 5, 5, 5)]; mainCollection = [[UICollectionView alloc] initWithFrame:kFrame collectionViewLayout:layout]; mainCollection.backgroundColor = backGroundColor; mainCollection.delegate = self; mainCollection.dataSource = self; [self.view addSubview:mainCollection]; [mainCollection registerClass:[SelectCell class] forCellWithReuseIdentifier:@"SelectCell"]; holderV = [[HolderView alloc] initWithFrame:mainCollection.frame]; [holderV freshBlock:^{ [self getTrainFiles]; }]; [self addV:holderV]; holderBar = [[UIView alloc] initWithFrame:CGRectMake(0, kSize.height - 50, kSize.width, 50)]; holderBar.backgroundColor = backGroundColor; [self.view addSubview:holderBar]; [holderBar setHidden:YES]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -2, kSize.width, 2)]; label.backgroundColor = lineColor; [holderBar addSubview:label]; label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 50)]; [label setText:@"您已选了0张照片噢" Font:20 TextColor:kTitleColor Alignment:NSTextAlignmentLeft]; [holderBar addSubview:label]; countLabel = label; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(kSize.width - 100, 7, 90, 36); [btn setTitle:@"确认删除" textColor:[UIColor whiteColor] font:20 fotState:UIControlStateNormal]; btn.layer.masksToBounds = YES; btn.layer.cornerRadius = 10; btn.backgroundColor = [UIColor orangeColor]; [btn target:self]; [holderBar addSubview:btn]; [self getTrainFiles]; } -(void)clickToDelete { isEdit = !isEdit; if (isEdit) { //编辑 所有图片去掉交互 item.title = @"取消"; holderBar.hidden = NO; mainCollection.height -= 52; }else{ //取消编辑 加上交互 item.title = @"编辑"; mainCollection.height += 52; holderBar.hidden = YES; } [mainCollection reloadData]; } -(void)btnClick:(UIButton *)sender { //NSLog(@"删除操作"); [self delStuPhotos]; //隐藏功能 暂不开通 } -(void)tapImage:(UITapGestureRecognizer *)tap { //通过dataArray获取urls NSMutableArray *photos = [NSMutableArray arrayWithCapacity:dataArray.count]; //NSMutableArray *urls = [NSMutableArray arrayWithCapacity:dataArray.count]; for (int i = 0; i < dataArray.count; i ++) { NSDictionary *dic = dataArray[i]; NSString *url = dic[@"FILEPATH"]; NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; NSString *str = [url stringByTrimmingCharactersInSet:whitespace]; if (str && ![str hasPrefix:@"http"]){ str = [imgPreFix stringByAppendingString:[url stringByTrimmingCharactersInSet:whitespace]]; } NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; SelectCell *cell = (SelectCell *)[mainCollection cellForItemAtIndexPath:indexPath]; MJPhoto *photo = [[MJPhoto alloc] init]; photo.url = [NSURL URLWithString:str]; // 图片路径 photo.srcImageView = cell.imageView; // 来源于哪个UIImageView [photos addObject:photo]; } // 2.显示相册 MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init]; browser.currentPhotoIndex = tap.view.tag; // 弹出相册时显示的第一张图片是? browser.photos = photos; // 设置所有的图片 [browser show]; } #pragma mark collection delegate -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return dataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { SelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectCell" forIndexPath:indexPath]; NSDictionary *dic = dataArray[indexPath.row]; NSString *str = dic[@"FILEPATH"]; NSString *photoString = @"http://fj.jppt.com.cn/photo/"; if (str && ![str hasPrefix:@"http"]){ str = [photoString stringByAppendingString:str]; } if (!str) { str = @""; } [cell.imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"noImg.png"]]; NSString *timeStr = dic[@"PTIME"]; if (timeStr.length > 16) { timeStr = [timeStr substringWithRange:NSMakeRange(5, 11)]; } cell.timeLabel.text = timeStr; cell.kemuLabel.text = [NSString stringWithFormat:@"相似度:%@%%",dic[@"FACEJUDE"]]; //拍照类型 // NSString *typeString = @""; // switch ([dic[@"TYPE"] integerValue]) { // case 1: // typeString = @"定时抓拍"; // break; // case 2: // typeString = @"签到抓拍"; // break; // case 3: // typeString = @"签退抓拍"; // break; // case 4: // typeString = @"随机指令抓拍"; // break; // default: // break; // } // cell.styleLabel.text = typeString; cell.imageView.tag = indexPath.row; if (isEdit) { cell.imageView.userInteractionEnabled = NO; }else{ cell.imageView.userInteractionEnabled = YES; } // 事件监听 [cell.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]]; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (!isEdit) { return; } //如果手势没用的时候 再开这个用来选中删除 SelectCell *cell = (SelectCell *)[mainCollection cellForItemAtIndexPath:indexPath]; cell.isDelete = !cell.isDelete; if (cell.isDelete) { cell.selectView.hidden = NO; [delArray addObject:dataArray[indexPath.row]]; }else{ cell.selectView.hidden = YES; if ([delArray containsObject:dataArray[indexPath.row]]) { [delArray removeObject:dataArray[indexPath.row]]; } } //NSLog(@"delArray-->%@",delArray); countLabel.text = [NSString stringWithFormat:@"您已选了%d张照片噢",(int)delArray.count]; } #pragma mark 数据请求 - (void)getTrainFiles { [LoadingView showHUD]; if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"classId" Value:_stuDic[@"classId"]]; [arr addPro:@"stuId" Value:_stuDic[@"TSO_ID"] ? _stuDic[@"TSO_ID"]:_stuDic[@"stuId"]];//不同地方传进来不一样 [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; NSString* method = @"getTrainFiles"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); [holderV setHidden:NO]; if (!root) { return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } if ([root[@"body"] count] == 0) { return; } dataArray = root[@"body"]; [holderV setHidden:YES]; [mainCollection reloadData]; }]; } - (void)getStuPhotos { [LoadingView showHUD]; if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"stuId" Value:_stuDic[@"TSO_ID"]]; [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; NSString* method = @"getStuPhotos"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); [holderV setHidden:NO]; if (!root) { return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } if ([root[@"body"] count] == 0) { return; } dataArray = root[@"body"]; [holderV setHidden:YES]; [mainCollection reloadData]; }]; } - (void)delStuPhotos { [LoadingView showHUD]; if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSString *delString = @""; for (NSDictionary *dic in delArray) { delString = [delString stringByAppendingString:[NSString stringWithFormat:@"%@,",dic[@"ID"]]]; } if (delString.length > 0) { delString = [delString substringToIndex:delString.length - 1]; } NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"ids" Value:delString]; [arr addPro:@"zjhm" Value:_stuDic[@"STU_SFZHM"]]; // NSString* method = @"delStuPhotos"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); if (!root) { ShowMsg(@"删除失败"); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } //成功要刷新 [self getStuPhotos]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end