ModelPhotoViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //
  2. // ModelPhotoViewController.m
  3. // model
  4. //
  5. // Created by liufei on 2018/7/25.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. //
  8. #import "ModelPhotoViewController.h"
  9. #import "PhotoCollectionViewCell.h"
  10. #import "PhotoModel.h"
  11. #import "Masonry.h"
  12. #import "UICollectionViewLeftAlignedLayout.h"
  13. @interface ModelPhotoViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
  14. UIImagePickerController *pickerVc;
  15. }
  16. @property(nonatomic, strong) UICollectionView *photoListView;
  17. @property(nonatomic, strong) NSMutableArray *dataSource;
  18. @property(nonatomic, strong) NSMutableArray *tempUrl;
  19. @property(nonatomic, strong) NSMutableArray *updateData;
  20. @property(nonatomic, strong) NSMutableArray *deleteData;
  21. @end
  22. @implementation ModelPhotoViewController
  23. - (NSMutableArray *)dataSource {
  24. if (!_dataSource) {
  25. _dataSource = [NSMutableArray array];
  26. }
  27. return _dataSource;
  28. }
  29. - (NSMutableArray *)updateData {
  30. if (!_updateData) {
  31. _updateData = [NSMutableArray array];
  32. }
  33. return _updateData;
  34. }
  35. - (NSMutableArray *)deleteData {
  36. if (!_deleteData) {
  37. _deleteData = [NSMutableArray array];
  38. }
  39. return _deleteData;
  40. }
  41. - (NSMutableArray *)tempUrl {
  42. if (!_tempUrl) {
  43. _tempUrl = [NSMutableArray array];
  44. }
  45. return _tempUrl;
  46. }
  47. - (void)viewDidLoad {
  48. [super viewDidLoad];
  49. [self configUI];
  50. [self getModelPhoto];
  51. }
  52. - (void)getModelPhoto {
  53. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  54. [YanCNetWorkManager requestGETWithURLStr:Url_getModelPhoto(PublicUrl)
  55. parameters:@{@"modelpk": [ModelUser user].modelpk}
  56. finish:^(id dataDic) {
  57. [MBProgressHUD hideHUDForView:self.view animated:YES];
  58. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  59. for (NSDictionary *dic in dataDic[@"data"]) {
  60. PhotoModel *model = [PhotoModel mj_objectWithKeyValues:dic];
  61. [self.dataSource addObject:model];
  62. }
  63. [self.photoListView reloadData];
  64. }
  65. }
  66. enError:^(NSError *error) {
  67. }];
  68. }
  69. - (void)configUI {
  70. self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F4F5"];
  71. self.title = @"照片";
  72. self.navigationController.navigationBar.tintColor = RGBValueColor(0x333333, 1);
  73. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave)];
  74. rightItem.tintColor = [UIColor blackColor];
  75. self.navigationItem.rightBarButtonItem = rightItem;
  76. UICollectionViewFlowLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
  77. self.photoListView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
  78. self.photoListView.delegate = self;
  79. self.photoListView.dataSource = self;
  80. self.photoListView.backgroundColor = [UIColor clearColor];
  81. [self.photoListView registerNib:[UINib nibWithNibName:@"PhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])];
  82. [self.photoListView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
  83. [self.view addSubview:self.photoListView];
  84. [self.photoListView mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.top.equalTo(self.view);
  86. make.bottom.equalTo(self.view);
  87. make.left.right.equalTo(self.view);
  88. }];
  89. }
  90. - (void)backClick {
  91. [self.navigationController popViewControllerAnimated:YES];
  92. }
  93. #pragma mark -collectionView
  94. - (CGSize)collectionView:(UICollectionView *)collectionView
  95. layout:(UICollectionViewLayout *)collectionViewLayout
  96. sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  97. if (indexPath.row == 0) {
  98. return CGSizeMake(ScreenWidth - 40, ScreenWidth - 40);
  99. } else {
  100. return CGSizeMake((ScreenWidth - 50) / 2, (ScreenWidth - 50) / 2);
  101. }
  102. }
  103. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
  104. layout:(UICollectionViewLayout *)collectionViewLayout
  105. insetForSectionAtIndex:(NSInteger)section {
  106. return UIEdgeInsetsMake(10, 20, 20, 20);
  107. }
  108. - (CGFloat) collectionView:(UICollectionView *)collectionView
  109. layout:(UICollectionViewLayout *)collectionViewLayout
  110. minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  111. return 10;
  112. }
  113. - (CGFloat) collectionView:(UICollectionView *)collectionView
  114. layout:(UICollectionViewLayout *)collectionViewLayout
  115. minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  116. return 10;
  117. }
  118. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  119. return self.dataSource.count + 1;
  120. }
  121. - (CGSize) collectionView:(UICollectionView *)collectionView
  122. layout:(UICollectionViewLayout *)collectionViewLayout
  123. referenceSizeForHeaderInSection:(NSInteger)section {
  124. return CGSizeMake(ScreenWidth, 35);
  125. }
  126. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
  127. viewForSupplementaryElementOfKind:(NSString *)kind
  128. atIndexPath:(NSIndexPath *)indexPath {
  129. if (kind == UICollectionElementKindSectionHeader) {
  130. UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
  131. for (UIView *view in [headerView subviews]) {
  132. [view removeFromSuperview];
  133. }
  134. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, ScreenWidth - 40, 20)];
  135. label.text = @"点击可删除照片(最少上传5张)";
  136. label.font = [UIFont systemFontOfSize:14];
  137. label.textColor = [UIColor colorWithHexString:@"#666666"];
  138. [headerView addSubview:label];
  139. return headerView;
  140. }
  141. return nil;
  142. }
  143. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  144. PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])
  145. forIndexPath:indexPath];
  146. cell.layer.cornerRadius = 8;
  147. cell.clipsToBounds = YES;
  148. if (indexPath.item < self.dataSource.count) {
  149. PhotoModel *model = self.dataSource[indexPath.item];
  150. if (model.localImage) {
  151. cell.userImageView.image = model.localImage;
  152. } else {
  153. [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", PublicUrl, model.photo]]];
  154. }
  155. } else {
  156. cell.userImageView.image = nil;
  157. }
  158. return cell;
  159. }
  160. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  161. if (self.dataSource.count == indexPath.item) {
  162. if (self.dataSource.count >= 9) {
  163. [MBProgressHUD showSuccess:@"最多不能超过9张"];
  164. } else {
  165. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  166. pickerVc = [[UIImagePickerController alloc] init];
  167. pickerVc.allowsEditing = YES;
  168. pickerVc.delegate = self;
  169. UIAlertAction *library = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
  170. pickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  171. [self presentViewController:pickerVc animated:YES completion:nil];
  172. }];
  173. UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
  174. pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
  175. [self presentViewController:pickerVc animated:YES completion:nil];
  176. }];
  177. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  178. [alertVc addAction:library];
  179. [alertVc addAction:camera];
  180. [alertVc addAction:cancel];
  181. [self presentViewController:alertVc animated:YES completion:nil];
  182. }
  183. } else {
  184. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定删除" preferredStyle:UIAlertControllerStyleAlert];
  185. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  186. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
  187. [self deletePhoto:indexPath.item];
  188. }];
  189. [alertVc addAction:cancelAction];
  190. [alertVc addAction:sureAction];
  191. [self presentViewController:alertVc animated:YES completion:nil];
  192. }
  193. }
  194. #pragma mark -imagePickerController
  195. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
  196. UIImage *image = info[@"UIImagePickerControllerEditedImage"];
  197. [picker dismissViewControllerAnimated:NO completion:nil];
  198. PhotoModel *model = [[PhotoModel alloc] init];
  199. model.localImage = image;
  200. NSData *photoData = UIImageJPEGRepresentation(image, 0.5);
  201. NSInteger length = [photoData length] / 1000;
  202. NSLog(@"图片大小:%ld Kb", (long) length);
  203. [self.dataSource addObject:model];
  204. [self.updateData addObject:image];
  205. [self.photoListView reloadData];
  206. [self uploadImage:image];
  207. }
  208. - (void)uploadImage:(UIImage *)image {
  209. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  210. [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl)
  211. fileData:image
  212. name:@"img"
  213. fileName:@"file.jpg"
  214. mimeType:@"image/jpeg"
  215. parameters:nil
  216. finish:^(id dataDic) {
  217. [MBProgressHUD hideHUDForView:self.view animated:YES];
  218. [self.tempUrl addObject:dataDic[@"img"]];
  219. }
  220. enError:^(NSError *error) {
  221. [MBProgressHUD hideHUDForView:self.view animated:YES];
  222. }];
  223. }
  224. #pragma mark -
  225. - (void)handleSave {
  226. [self uploadImageThroughURLs:self.tempUrl];
  227. // [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  228. // dispatch_group_t group = dispatch_group_create();
  229. //
  230. // for (NSInteger i = 0; i < self.updateData.count; i++) {
  231. //
  232. // dispatch_group_enter(group);
  233. //
  234. // [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl) fileData:self.updateData[i] name:@"img" fileName:@"file.jpg" mimeType:@"image/jpeg" parameters:nil finish:^(id dataDic) {
  235. //
  236. // [self.tempUrl addObject:dataDic[@"img"]];
  237. //
  238. // NSLog(@"第 %d 张图片上传成功: %@", (int)i + 1, dataDic);
  239. // dispatch_group_leave(group);
  240. // } enError:^(NSError *error) {
  241. // NSLog(@"第 %d 张图片上传失败: %@", (int)i + 1, error);
  242. // dispatch_group_leave(group);
  243. // }];
  244. // }
  245. // [MBProgressHUD hideHUDForView:self.view animated:YES];
  246. // dispatch_group_notify(group, dispatch_get_main_queue(), ^{
  247. // NSLog(@"上传完成!");
  248. //
  249. // [self uploadImageThroughURLs:self.tempUrl];
  250. // });
  251. }
  252. - (void)uploadImageThroughURLs:(NSMutableArray *)urls {
  253. NSString *urlStr = @"";
  254. if (urls.count > 0) {
  255. urlStr = [urls firstObject];
  256. if (urls.count > 1) {
  257. for (int i = 1; i < urls.count; i++) {
  258. urlStr = [urlStr stringByAppendingFormat:@",%@", urls[i]];
  259. }
  260. }
  261. NSDictionary *dic = @{@"modelpk": [ModelUser user].modelpk, @"photo": urlStr};
  262. [YanCNetWorkManager requestPostWithURLStr:Url_addModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
  263. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  264. [MBProgressHUD showSuccess:@"保存成功"];
  265. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
  266. }
  267. } enError:^(NSError *error) {
  268. }];
  269. } else {
  270. [MBProgressHUD showSuccess:@"保存成功"];
  271. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
  272. }
  273. }
  274. - (void)deletePhoto:(NSInteger)index {
  275. PhotoModel *model = self.dataSource[index];
  276. if (model.localImage) {
  277. for (UIImage *img in self.updateData) {
  278. if (img == model.localImage) {
  279. [self.updateData removeObject:img];
  280. }
  281. }
  282. } else {
  283. NSDictionary *dic = @{@"modelpk": [ModelUser user].modelpk, @"modelphotopk": model.modelphotopk};
  284. [YanCNetWorkManager requestPostWithURLStr:Url_deleteModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
  285. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  286. }
  287. } enError:^(NSError *error) {
  288. }];
  289. }
  290. [self.dataSource removeObjectAtIndex:index];
  291. [self.photoListView reloadData];
  292. }
  293. - (void)didReceiveMemoryWarning {
  294. [super didReceiveMemoryWarning];
  295. // Dispose of any resources that can be recreated.
  296. }
  297. /*
  298. #pragma mark - Navigation
  299. // In a storyboard-based application, you will often want to do a little preparation before navigation
  300. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  301. // Get the new view controller using [segue destinationViewController].
  302. // Pass the selected object to the new view controller.
  303. }
  304. */
  305. @end