SelectImgVC.m 11 KB

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