ModelPhotoViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. @interface ModelPhotoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  12. {
  13. UIImagePickerController *pickerVc ;
  14. }
  15. @property (nonatomic, strong) UICollectionView *photoListView;
  16. @property (nonatomic, strong) NSMutableArray *dataSource;
  17. @property (nonatomic, strong) UIButton *saveBtn;
  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) parameters:@{@"modelpk":[ModelUser user].modelpk} finish:^(id dataDic) {
  55. [MBProgressHUD hideHUDForView:self.view animated:YES];
  56. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  57. for (NSDictionary *dic in dataDic[@"data"]) {
  58. PhotoModel *model = [PhotoModel mj_objectWithKeyValues:dic];
  59. [self.dataSource addObject:model];
  60. }
  61. [self.photoListView reloadData];
  62. }
  63. } enError:^(NSError *error) {
  64. }];
  65. }
  66. - (void)configUI {
  67. self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  68. self.title = @"照片";
  69. self.navigationController.navigationBar.tintColor = RGBValueColor(0x333333, 1);
  70. //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave)];
  71. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  72. layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
  73. layout.itemSize = CGSizeMake(ScreenWidth/2 - 25, ScreenWidth/2 - 25);
  74. layout.minimumLineSpacing = 10;
  75. layout.minimumInteritemSpacing = 10;
  76. self.photoListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth) collectionViewLayout:layout];
  77. self.photoListView.delegate = self;
  78. self.photoListView.dataSource = self;
  79. self.photoListView.backgroundColor = [UIColor whiteColor];
  80. [self.photoListView registerNib:[UINib nibWithNibName:@"PhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])];
  81. [self.view addSubview:self.photoListView];
  82. UILabel *deleteLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.photoListView.frame), 200, 20)];
  83. deleteLabel.text = @"点击可删除照片";
  84. deleteLabel.textColor = RGB(102, 102, 102);
  85. deleteLabel.font = [UIFont systemFontOfSize:14];
  86. [self.view addSubview:deleteLabel];
  87. self.saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(20,CGRectGetMaxY(self.photoListView.frame) + 40, ScreenWidth - 40, 48)];
  88. self.saveBtn.layer.cornerRadius = 24.f;
  89. self.saveBtn.layer.masksToBounds = YES;
  90. [self.saveBtn setTitle:@"保存" forState:UIControlStateNormal];
  91. [self.saveBtn addTarget:self action:@selector(handleSave) forControlEvents:UIControlEventTouchUpInside];
  92. [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"tixian-1"] forState:UIControlStateNormal];
  93. [self.view addSubview:self.saveBtn];
  94. }
  95. - (void)backClick {
  96. [self.navigationController popViewControllerAnimated:YES];
  97. }
  98. #pragma mark -collectionView
  99. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  100. return self.dataSource.count+1;
  101. }
  102. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  103. PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class]) forIndexPath:indexPath];
  104. if (indexPath.item < self.dataSource.count) {
  105. PhotoModel *model = self.dataSource[indexPath.item];
  106. if (model.localImage) {
  107. cell.userImageView.image = model.localImage;
  108. } else {
  109. [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", PublicUrl,model.photo]]];
  110. }
  111. }else{
  112. cell.userImageView.image = [UIImage imageNamed:@"photo_add"];
  113. }
  114. return cell;
  115. }
  116. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. if (self.dataSource.count == indexPath.item) {
  119. if (self.dataSource.count >= 9) {
  120. [MBProgressHUD showSuccess:@"最多不能超过9张"];
  121. }else{
  122. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  123. pickerVc = [[UIImagePickerController alloc] init];
  124. pickerVc.allowsEditing = YES;
  125. pickerVc.delegate = self;
  126. UIAlertAction *library = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  127. pickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  128. [self presentViewController:pickerVc animated:YES completion:nil];
  129. }];
  130. UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  131. pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
  132. [self presentViewController:pickerVc animated:YES completion:nil];
  133. }];
  134. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  135. [alertVc addAction:library];
  136. [alertVc addAction:camera];
  137. [alertVc addAction:cancel];
  138. [self presentViewController:alertVc animated:YES completion:nil];
  139. }
  140. }else{
  141. UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定删除" preferredStyle:UIAlertControllerStyleAlert];
  142. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  143. UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  144. [self deletePhoto:indexPath.item];
  145. }];
  146. [alertVc addAction:cancelAction];
  147. [alertVc addAction:sureAction];
  148. [self presentViewController:alertVc animated:YES completion:nil];
  149. }
  150. }
  151. #pragma mark -imagePickerController
  152. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
  153. UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
  154. [picker dismissViewControllerAnimated:NO completion:nil];
  155. PhotoModel *model = [[PhotoModel alloc] init];
  156. model.localImage = image;
  157. NSData *photoData = UIImageJPEGRepresentation(image, 0.5);
  158. NSInteger length = [photoData length] / 1000;
  159. NSLog(@"图片大小:%ld Kb",(long)length);
  160. [self.dataSource addObject:model];
  161. [self.updateData addObject:image];
  162. [self.photoListView reloadData];
  163. [self uploadImage:image];
  164. }
  165. -(void)uploadImage:(UIImage *)image{
  166. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  167. [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl) fileData:image name:@"img" fileName:@"file.jpg" mimeType:@"image/jpeg" parameters:nil finish:^(id dataDic) {
  168. [MBProgressHUD hideHUDForView:self.view animated:YES];
  169. [self.tempUrl addObject:dataDic[@"img"]];
  170. } enError:^(NSError *error) {
  171. [MBProgressHUD hideHUDForView:self.view animated:YES];
  172. }];
  173. }
  174. #pragma mark -
  175. - (void)handleSave {
  176. [self uploadImageThroughURLs:self.tempUrl];
  177. // [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  178. // dispatch_group_t group = dispatch_group_create();
  179. //
  180. // for (NSInteger i = 0; i < self.updateData.count; i++) {
  181. //
  182. // dispatch_group_enter(group);
  183. //
  184. // [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl) fileData:self.updateData[i] name:@"img" fileName:@"file.jpg" mimeType:@"image/jpeg" parameters:nil finish:^(id dataDic) {
  185. //
  186. // [self.tempUrl addObject:dataDic[@"img"]];
  187. //
  188. // NSLog(@"第 %d 张图片上传成功: %@", (int)i + 1, dataDic);
  189. // dispatch_group_leave(group);
  190. // } enError:^(NSError *error) {
  191. // NSLog(@"第 %d 张图片上传失败: %@", (int)i + 1, error);
  192. // dispatch_group_leave(group);
  193. // }];
  194. // }
  195. // [MBProgressHUD hideHUDForView:self.view animated:YES];
  196. // dispatch_group_notify(group, dispatch_get_main_queue(), ^{
  197. // NSLog(@"上传完成!");
  198. //
  199. // [self uploadImageThroughURLs:self.tempUrl];
  200. // });
  201. }
  202. -(void)uploadImageThroughURLs:(NSMutableArray *)urls{
  203. NSString *urlStr = @"";
  204. if (urls.count > 0) {
  205. urlStr = [urls firstObject];
  206. if (urls.count > 1) {
  207. for (int i=1; i<urls.count; i++) {
  208. urlStr = [urlStr stringByAppendingFormat:@",%@",urls[i]];
  209. }
  210. }
  211. NSDictionary *dic = @{@"modelpk":[ModelUser user].modelpk,@"photo":urlStr};
  212. [YanCNetWorkManager requestPostWithURLStr:Url_addModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
  213. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  214. [MBProgressHUD showSuccess:@"保存成功"];
  215. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
  216. }
  217. } enError:^(NSError *error) {
  218. }];
  219. }else{
  220. [MBProgressHUD showSuccess:@"保存成功"];
  221. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
  222. }
  223. }
  224. -(void)deletePhoto:(NSInteger)index{
  225. PhotoModel *model = [self.dataSource objectAtIndex:index];
  226. if (model.localImage) {
  227. for (UIImage *img in self.updateData ) {
  228. if (img == model.localImage) {
  229. [self.updateData removeObject:img];
  230. }
  231. }
  232. }else{
  233. NSDictionary *dic = @{@"modelpk":[ModelUser user].modelpk,@"modelphotopk":model.modelphotopk};
  234. [YanCNetWorkManager requestPostWithURLStr:Url_deleteModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
  235. if ([dataDic[@"msg"] isEqualToString:@"success"]) {
  236. }
  237. } enError:^(NSError *error) {
  238. }];
  239. }
  240. [self.dataSource removeObjectAtIndex:index];
  241. [self.photoListView reloadData];
  242. }
  243. - (void)didReceiveMemoryWarning {
  244. [super didReceiveMemoryWarning];
  245. // Dispose of any resources that can be recreated.
  246. }
  247. /*
  248. #pragma mark - Navigation
  249. // In a storyboard-based application, you will often want to do a little preparation before navigation
  250. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  251. // Get the new view controller using [segue destinationViewController].
  252. // Pass the selected object to the new view controller.
  253. }
  254. */
  255. @end