SelectImgVC.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // SelectImgVC.m
  3. // jiaPeiC
  4. //
  5. // Created by apple on 16/6/30.
  6. // Copyright © 2016年 JCZ. All rights reserved.
  7. //
  8. #import "SelectImgVC.h"
  9. #import "SelectCell.h"
  10. #import "HolderView.h"
  11. #import "STTButton.h"
  12. #import "MJPhotoBrowser.h"
  13. #import "MJPhoto.h"
  14. @interface SelectImgVC ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
  15. {
  16. HolderView *holderV;
  17. UICollectionView *mainCollection;
  18. UIBarButtonItem *item;
  19. UIView *holderBar;
  20. UILabel *countLabel;
  21. NSMutableArray *dataArray;
  22. NSMutableArray *delArray;
  23. BOOL isEdit;
  24. }
  25. @end
  26. @implementation SelectImgVC
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.title = [NSString stringWithFormat:@"%@的照片",_stuDic[@"STU_NAME"]? _stuDic[@"STU_NAME"]:_stuDic[@"STUNAME"]];
  30. self.view.backgroundColor = backGroundColor;
  31. dataArray = [NSMutableArray array];
  32. delArray = [NSMutableArray array];
  33. // item = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(clickToDelete)];
  34. // [item setTintColor:defGreen];
  35. // [self.navigationItem setRightBarButtonItem:item];
  36. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  37. CGFloat width = (kSize.width - 30)/3.0;
  38. [layout setItemSize:CGSizeMake(width, width + 60)];
  39. [layout setSectionInset:UIEdgeInsetsMake(10, 5, 5, 5)];
  40. mainCollection = [[UICollectionView alloc] initWithFrame:kFrame collectionViewLayout:layout];
  41. mainCollection.backgroundColor = backGroundColor;
  42. mainCollection.delegate = self;
  43. mainCollection.dataSource = self;
  44. [self.view addSubview:mainCollection];
  45. [mainCollection registerClass:[SelectCell class] forCellWithReuseIdentifier:@"SelectCell"];
  46. holderV = [[HolderView alloc] initWithFrame:mainCollection.frame];
  47. [holderV freshBlock:^{
  48. [self getTrainFiles];
  49. }];
  50. [self addV:holderV];
  51. holderBar = [[UIView alloc] initWithFrame:CGRectMake(0, kSize.height - 50, kSize.width, 50)];
  52. holderBar.backgroundColor = backGroundColor;
  53. [self.view addSubview:holderBar];
  54. [holderBar setHidden:YES];
  55. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, -2, kSize.width, 2)];
  56. label.backgroundColor = lineColor;
  57. [holderBar addSubview:label];
  58. label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 50)];
  59. [label setText:@"您已选了0张照片噢" Font:20 TextColor:kTitleColor Alignment:NSTextAlignmentLeft];
  60. [holderBar addSubview:label];
  61. countLabel = label;
  62. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  63. btn.frame = CGRectMake(kSize.width - 100, 7, 90, 36);
  64. [btn setTitle:@"确认删除" textColor:[UIColor whiteColor] font:20 fotState:UIControlStateNormal];
  65. btn.layer.masksToBounds = YES;
  66. btn.layer.cornerRadius = 10;
  67. btn.backgroundColor = [UIColor orangeColor];
  68. [btn target:self];
  69. [holderBar addSubview:btn];
  70. [self getTrainFiles];
  71. }
  72. -(void)clickToDelete
  73. {
  74. isEdit = !isEdit;
  75. if (isEdit) {
  76. //编辑 所有图片去掉交互
  77. item.title = @"取消";
  78. holderBar.hidden = NO;
  79. mainCollection.height -= 52;
  80. }else{
  81. //取消编辑 加上交互
  82. item.title = @"编辑";
  83. mainCollection.height += 52;
  84. holderBar.hidden = YES;
  85. }
  86. [mainCollection reloadData];
  87. }
  88. -(void)btnClick:(UIButton *)sender
  89. {
  90. //NSLog(@"删除操作");
  91. [self delStuPhotos]; //隐藏功能 暂不开通
  92. }
  93. -(void)tapImage:(UITapGestureRecognizer *)tap
  94. {
  95. //通过dataArray获取urls
  96. NSMutableArray *photos = [NSMutableArray arrayWithCapacity:dataArray.count];
  97. //NSMutableArray *urls = [NSMutableArray arrayWithCapacity:dataArray.count];
  98. for (int i = 0; i < dataArray.count; i ++) {
  99. NSDictionary *dic = dataArray[i];
  100. NSString *url = dic[@"FILEPATH"];
  101. NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
  102. NSString *str = [url stringByTrimmingCharactersInSet:whitespace];
  103. if (str && ![str hasPrefix:@"http"]){
  104. str = [imgPreFix stringByAppendingString:[url stringByTrimmingCharactersInSet:whitespace]];
  105. }
  106. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
  107. SelectCell *cell = (SelectCell *)[mainCollection cellForItemAtIndexPath:indexPath];
  108. MJPhoto *photo = [[MJPhoto alloc] init];
  109. photo.url = [NSURL URLWithString:str]; // 图片路径
  110. photo.srcImageView = cell.imageView; // 来源于哪个UIImageView
  111. [photos addObject:photo];
  112. }
  113. // 2.显示相册
  114. MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
  115. browser.currentPhotoIndex = tap.view.tag; // 弹出相册时显示的第一张图片是?
  116. browser.photos = photos; // 设置所有的图片
  117. [browser show];
  118. }
  119. #pragma mark collection delegate
  120. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  121. {
  122. return dataArray.count;
  123. }
  124. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. SelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelectCell" forIndexPath:indexPath];
  127. NSDictionary *dic = dataArray[indexPath.row];
  128. NSString *str = dic[@"FILEPATH"];
  129. NSString *photoString = @"http://fj.jppt.com.cn/photo/";
  130. if (str && ![str hasPrefix:@"http"]){
  131. str = [photoString stringByAppendingString:str];
  132. }
  133. if (!str) {
  134. str = @"";
  135. }
  136. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"noImg.png"]];
  137. NSString *timeStr = dic[@"PTIME"];
  138. if (timeStr.length > 16) {
  139. timeStr = [timeStr substringWithRange:NSMakeRange(5, 11)];
  140. }
  141. cell.timeLabel.text = timeStr;
  142. cell.kemuLabel.text = [NSString stringWithFormat:@"相似度:%@%%",dic[@"FACEJUDE"]];
  143. //拍照类型
  144. // NSString *typeString = @"";
  145. // switch ([dic[@"TYPE"] integerValue]) {
  146. // case 1:
  147. // typeString = @"定时抓拍";
  148. // break;
  149. // case 2:
  150. // typeString = @"签到抓拍";
  151. // break;
  152. // case 3:
  153. // typeString = @"签退抓拍";
  154. // break;
  155. // case 4:
  156. // typeString = @"随机指令抓拍";
  157. // break;
  158. // default:
  159. // break;
  160. // }
  161. // cell.styleLabel.text = typeString;
  162. cell.imageView.tag = indexPath.row;
  163. if (isEdit) {
  164. cell.imageView.userInteractionEnabled = NO;
  165. }else{
  166. cell.imageView.userInteractionEnabled = YES;
  167. }
  168. // 事件监听
  169. [cell.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)]];
  170. return cell;
  171. }
  172. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. if (!isEdit) {
  175. return;
  176. }
  177. //如果手势没用的时候 再开这个用来选中删除
  178. SelectCell *cell = (SelectCell *)[mainCollection cellForItemAtIndexPath:indexPath];
  179. cell.isDelete = !cell.isDelete;
  180. if (cell.isDelete) {
  181. cell.selectView.hidden = NO;
  182. [delArray addObject:dataArray[indexPath.row]];
  183. }else{
  184. cell.selectView.hidden = YES;
  185. if ([delArray containsObject:dataArray[indexPath.row]]) {
  186. [delArray removeObject:dataArray[indexPath.row]];
  187. }
  188. }
  189. //NSLog(@"delArray-->%@",delArray);
  190. countLabel.text = [NSString stringWithFormat:@"您已选了%d张照片噢",(int)delArray.count];
  191. }
  192. #pragma mark 数据请求
  193. - (void)getTrainFiles
  194. {
  195. [LoadingView showHUD];
  196. if (![Util connectedToNetWork]) {
  197. showMsgUnconnect();
  198. return;
  199. }
  200. NSMutableArray *arr = [NSMutableArray array];
  201. [arr addPro:@"classId" Value:_stuDic[@"classId"]];
  202. [arr addPro:@"stuId" Value:_stuDic[@"TSO_ID"] ? _stuDic[@"TSO_ID"]:_stuDic[@"stuId"]];//不同地方传进来不一样
  203. [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]];
  204. NSString* method = @"getTrainFiles";
  205. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  206. RemoveHUD();
  207. [holderV setHidden:NO];
  208. if (!root) {
  209. return;
  210. }
  211. if ([root[@"code"] isEqualToString:@"1"]) {
  212. ShowMsg(root[@"body"]);
  213. return;
  214. }
  215. if ([root[@"body"] count] == 0) {
  216. return;
  217. }
  218. dataArray = root[@"body"];
  219. [holderV setHidden:YES];
  220. [mainCollection reloadData];
  221. }];
  222. }
  223. - (void)getStuPhotos
  224. {
  225. [LoadingView showHUD];
  226. if (![Util connectedToNetWork]) {
  227. showMsgUnconnect();
  228. return;
  229. }
  230. NSMutableArray *arr = [NSMutableArray array];
  231. [arr addPro:@"stuId" Value:_stuDic[@"TSO_ID"]];
  232. [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]];
  233. NSString* method = @"getStuPhotos";
  234. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  235. RemoveHUD();
  236. [holderV setHidden:NO];
  237. if (!root) {
  238. return;
  239. }
  240. if ([root[@"code"] isEqualToString:@"1"]) {
  241. ShowMsg(root[@"body"]);
  242. return;
  243. }
  244. if ([root[@"body"] count] == 0) {
  245. return;
  246. }
  247. dataArray = root[@"body"];
  248. [holderV setHidden:YES];
  249. [mainCollection reloadData];
  250. }];
  251. }
  252. - (void)delStuPhotos
  253. {
  254. [LoadingView showHUD];
  255. if (![Util connectedToNetWork]) {
  256. showMsgUnconnect();
  257. return;
  258. }
  259. NSString *delString = @"";
  260. for (NSDictionary *dic in delArray) {
  261. delString = [delString stringByAppendingString:[NSString stringWithFormat:@"%@,",dic[@"ID"]]];
  262. }
  263. if (delString.length > 0) {
  264. delString = [delString substringToIndex:delString.length - 1];
  265. }
  266. NSMutableArray *arr = [NSMutableArray array];
  267. [arr addPro:@"ids" Value:delString];
  268. [arr addPro:@"zjhm" Value:_stuDic[@"STU_SFZHM"]]; //
  269. NSString* method = @"delStuPhotos";
  270. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) {
  271. RemoveHUD();
  272. if (!root) {
  273. ShowMsg(@"删除失败");
  274. return;
  275. }
  276. if ([root[@"code"] isEqualToString:@"1"]) {
  277. ShowMsg(root[@"body"]);
  278. return;
  279. }
  280. //成功要刷新
  281. [self getStuPhotos];
  282. }];
  283. }
  284. - (void)didReceiveMemoryWarning {
  285. [super didReceiveMemoryWarning];
  286. }
  287. @end