PhotosUploadViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // PhotosUploadViewController.m
  3. // LN_School
  4. //
  5. // Created by 张嵘 on 2018/10/10.
  6. // Copyright © 2018 Danson. All rights reserved.
  7. //
  8. #import "PhotosUploadViewController.h"
  9. #import "PureCamera.h"
  10. #import "PhotosUploadCell.h"
  11. typedef void (^UploadImageBlock) (NSArray *imgPathArr);
  12. typedef NS_ENUM(NSInteger, EditImgType) {
  13. EditImgType_Add = 0,
  14. EditImgType_Replace,
  15. };
  16. @interface PhotosUploadViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UIGestureRecognizerDelegate, UIScrollViewDelegate>
  17. @property (nonatomic, strong) ReturnImagesBlock returnImagesBlock;
  18. @property (nonatomic, strong) NSMutableArray *imagesArr;
  19. @property (nonatomic, strong) NSMutableArray *imagesPathArr;
  20. @property (nonatomic, strong) UILabel *pictureNumberLabel;
  21. @property (nonatomic, assign) CGFloat itemWith;
  22. @property (nonatomic, assign) EditImgType editImgType;
  23. @property (nonatomic, assign) NSInteger currentTag;
  24. @end
  25. static NSString *collectionIdentifer = @"PhotosUploadCell";
  26. @implementation PhotosUploadViewController
  27. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil imagesArr:(NSArray *)imagesArr imagesPathArr:(NSArray *)imagesPathArr returnImagesBlock:(ReturnImagesBlock)returnImagesBlock {
  28. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  29. if (self) {
  30. _returnImagesBlock = returnImagesBlock;
  31. if (imagesArr) {
  32. self.imagesArr = [NSMutableArray arrayWithArray:imagesArr];
  33. }else {
  34. self.imagesArr = [NSMutableArray array];
  35. }
  36. if (imagesPathArr) {
  37. self.imagesPathArr = [NSMutableArray arrayWithArray:imagesPathArr];
  38. }else {
  39. self.imagesPathArr = [NSMutableArray array];
  40. }
  41. }
  42. return self;
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. self.title = @"图片上传";
  47. self.view.backgroundColor = KBackGroundColor;
  48. [self goBackByNavigation];
  49. [self initCollectView];
  50. [self.view addSubview:self.pictureNumberLabel];
  51. [self reactiveObjCBind];
  52. }
  53. //返回按钮点击事件
  54. - (void)goBackByNav {
  55. if (_imagesArr.count > 0) {
  56. [RQ_SHARE_FUNCTION showAlertWithTitle:@"是否保存当前图片" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) {
  57. if (selectedOtherButtonIndex == 0) {
  58. if (_returnImagesBlock) {
  59. _returnImagesBlock(_imagesArr, self.imagesPathArr);
  60. }
  61. [self.view endEditing:1];
  62. [self.navigationController popViewControllerAnimated:YES];
  63. }else if (selectedOtherButtonIndex == NSNotFound) {
  64. [self.view endEditing:1];
  65. [self.navigationController popViewControllerAnimated:YES];
  66. }
  67. }];
  68. }else {
  69. [self.view endEditing:1];
  70. [self.navigationController popViewControllerAnimated:YES];
  71. }
  72. }
  73. #pragma mark - UICollectionViewDataSource
  74. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  75. return 1;
  76. }
  77. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  78. return self.imagesArr.count + 1;
  79. }
  80. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  81. PhotosUploadCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionIdentifer forIndexPath:indexPath];
  82. if (cell==nil) {
  83. cell = [[[NSBundle mainBundle] loadNibNamed:@"PhotosUploadCell" owner:nil options:nil] lastObject];
  84. }
  85. cell.deleteBtn.hidden = YES;
  86. if (indexPath.row == self.imagesArr.count) {
  87. [cell.bgImg setImage:[UIImage imageNamed:@"AddImage"]];
  88. }else{
  89. // [cell.bgImg sd_setImageWithURL:[NSURL URLWithString:self.imagesArr[indexPath.row]] placeholderImage:[UIImage imageNamed:@"AddImage"]];
  90. cell.bgImg.image = self.imagesArr[indexPath.row];
  91. cell.deleteBtn.tag = 1000 + indexPath.row;
  92. cell.deleteBtn.hidden = NO;
  93. [cell.deleteBtn addTarget:self action:@selector(removeAction:) forControlEvents:UIControlEventTouchUpInside];
  94. }
  95. return cell;
  96. }
  97. #pragma mark - UICollectionViewDelegate
  98. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  99. if (indexPath.row == self.imagesArr.count) {
  100. //添加
  101. if (self.imagesArr.count >= 6) {
  102. ShowMsg(@"最多选择6张图片");
  103. return;
  104. }
  105. [self takePhotoWithTag:indexPath.row editImgType:EditImgType_Add];
  106. }else{
  107. //替换
  108. [self takePhotoWithTag:indexPath.row editImgType:EditImgType_Replace];
  109. }
  110. }
  111. #pragma mark - Custom Way
  112. //绑定监听数组内容信号
  113. - (void)reactiveObjCBind {
  114. [RACObserve(self, imagesArr) subscribeNext:^(id _Nullable x) {
  115. NSArray *arr = x;
  116. _pictureNumberLabel.text = [NSString stringWithFormat:@"添加图片%lu/6 ",(unsigned long)arr.count];
  117. [self.collectView reloadData];
  118. }];
  119. }
  120. //保存按钮点击事件
  121. - (IBAction)saveBtnAction:(id)sender {
  122. if (_imagesArr.count > 0) {
  123. [RQ_SHARE_FUNCTION showAlertWithTitle:@"保存图片并返回上一页" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) {
  124. if (selectedOtherButtonIndex == 0) {
  125. if (_returnImagesBlock) {
  126. _returnImagesBlock(_imagesArr, self.imagesPathArr);
  127. }
  128. [self.view endEditing:1];
  129. [self.navigationController popViewControllerAnimated:YES];
  130. }
  131. }];
  132. }else {
  133. ShowMsg(@"请上传图片");
  134. }
  135. }
  136. //移除图片
  137. - (void)removeAction:(UIButton*)sender {
  138. [RQ_SHARE_FUNCTION showAlertWithTitle:@"是否删除这张图片" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) {
  139. if (selectedOtherButtonIndex == 0) {
  140. [[self mutableArrayValueForKey:@"imagesArr"] removeObjectAtIndex:sender.tag - 1000];
  141. [[self mutableArrayValueForKey:@"imagesPathArr"] removeObjectAtIndex:sender.tag - 1000];
  142. }
  143. }];
  144. }
  145. //collecViews设置
  146. - (void)initCollectView {
  147. _itemWith = kScreenWidth / 4;
  148. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  149. layout.itemSize = CGSizeMake(_itemWith - 10, _itemWith - 10);
  150. layout.minimumLineSpacing = 10;
  151. layout.minimumInteritemSpacing = 0;
  152. layout.sectionInset = UIEdgeInsetsMake(23, 10, 0, 10);//上左下右
  153. [self.collectView setCollectionViewLayout:layout];
  154. [self.collectView registerNib:[UINib nibWithNibName:collectionIdentifer bundle:nil] forCellWithReuseIdentifier:collectionIdentifer];
  155. _collectView.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.00];
  156. }
  157. - (void)takePhotoWithTag:(NSInteger)tag editImgType:(EditImgType)editImgType { //100*120
  158. self.currentTag = tag;
  159. self.editImgType = editImgType;
  160. [RQ_SHARE_FUNCTION showAlertWithTitle:@"请选择获取图片方式" message:nil alertControllerStyle:UIAlertControllerStyleActionSheet cancelButtonTitle:@"取消" otherButtonTitles:@[@"拍照", @"本地照片"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) {
  161. if (selectedOtherButtonIndex == 0) {
  162. [RQ_SHARE_FUNCTION getPhotosWithGetPhotosWay:GetPhotosWay_Camera size:CGSizeZero maxLength:100 maxImagesCount:1 photosBlock:^(NSArray * _Nonnull imagesArr, NSArray * _Nonnull imagesDataStrArr) {
  163. [self uploadHeadImg:imagesDataStrArr completeBlock:^(NSArray *imgPathArr) {
  164. if (editImgType == EditImgType_Add) {
  165. for (UIImage *image in imagesArr) {
  166. [[self mutableArrayValueForKey:@"imagesArr"] addObject:image];
  167. }
  168. for (NSString *imgPath in imgPathArr) {
  169. [[self mutableArrayValueForKey:@"imagesPathArr"] addObject:imgPath];
  170. }
  171. }else if (editImgType == EditImgType_Replace && imgPathArr.count > 0) {
  172. [[self mutableArrayValueForKey:@"imagesArr"] replaceObjectAtIndex:tag withObject:imagesArr[0]];
  173. [[self mutableArrayValueForKey:@"imagesPathArr"] replaceObjectAtIndex:tag withObject:imgPathArr[0]];
  174. }
  175. }];
  176. }];
  177. }else if (selectedOtherButtonIndex == 1) {
  178. [RQ_SHARE_FUNCTION getPhotosWithGetPhotosWay:GetPhotosWay_Album size:CGSizeZero maxLength:100 maxImagesCount:editImgType == EditImgType_Replace? 1 : 6 photosBlock:^(NSArray * _Nonnull imagesArr, NSArray * _Nonnull imagesDataStrArr) {
  179. [self uploadHeadImg:imagesDataStrArr completeBlock:^(NSArray *imgPathArr) {
  180. if (editImgType == EditImgType_Add) {
  181. for (UIImage *image in imagesArr) {
  182. [[self mutableArrayValueForKey:@"imagesArr"] addObject:image];
  183. }
  184. for (NSString *imgPath in imgPathArr) {
  185. [[self mutableArrayValueForKey:@"imagesPathArr"] addObject:imgPath];
  186. }
  187. }else if (editImgType == EditImgType_Replace && imgPathArr.count > 0) {
  188. [[self mutableArrayValueForKey:@"imagesArr"] replaceObjectAtIndex:tag withObject:imagesArr[0]];
  189. [[self mutableArrayValueForKey:@"imagesPathArr"] replaceObjectAtIndex:tag withObject:imgPathArr[0]];
  190. }
  191. }];
  192. }];
  193. }
  194. }];
  195. }
  196. #pragma mark - Request Data
  197. - (void)uploadHeadImg:(NSArray *)imageArr completeBlock:(UploadImageBlock)uploadImageBlock {
  198. if (![NetManager connectedToNetWork]) {
  199. showMsgUnconnect();
  200. return;
  201. }
  202. //@RQ-MARK 1.0.2修改:“学员报名”模块新增 协议上传(上传多张)
  203. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  204. [dic setObject:imageArr.count > 1? [imageArr componentsJoinedByString:@","] : imageArr[0] forKey:@"content"];
  205. NSString *method = @"uploadStuProtocol";
  206. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
  207. if (!root) {
  208. [self showUPloadImgAlertWithImg:imageArr];
  209. return;
  210. }
  211. if ([root[@"code"] isEqualToString:@"1"]) {
  212. ShowMsg(root[@"msg"]);
  213. [self showUPloadImgAlertWithImg:imageArr];
  214. return;
  215. }
  216. //上传成功
  217. if (uploadImageBlock && root[@"body"]) {
  218. if ([root[@"body"] containsString:@","]) {
  219. uploadImageBlock([root[@"body"] componentsSeparatedByString:@","]);
  220. }else {
  221. uploadImageBlock(@[root[@"body"]]);
  222. }
  223. }
  224. }];
  225. }
  226. - (void)showUPloadImgAlertWithImg:(NSArray *)imageArr {
  227. __block NSArray *arr = imageArr;
  228. [RQ_SHARE_FUNCTION showAlertWithTitle:@"图片上传失败!" message:nil alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[@"再试一次"] otherButtonStyles:nil completion:^(NSUInteger selectedOtherButtonIndex) {
  229. if (selectedOtherButtonIndex == 0) {
  230. [self uploadHeadImg:arr completeBlock:^(NSArray *imgPathArr) {
  231. if (self.editImgType == EditImgType_Add) {
  232. for (UIImage *image in arr) {
  233. [[self mutableArrayValueForKey:@"imagesArr"] addObject:image];
  234. }
  235. for (NSString *imgPath in imgPathArr) {
  236. [[self mutableArrayValueForKey:@"imagesPathArr"] addObject:imgPath];
  237. }
  238. }else if (self.editImgType == EditImgType_Replace && imgPathArr.count > 0) {
  239. [[self mutableArrayValueForKey:@"imagesArr"] replaceObjectAtIndex:self.currentTag withObject:arr[0]];
  240. [[self mutableArrayValueForKey:@"imagesPathArr"] replaceObjectAtIndex:self.currentTag withObject:imgPathArr[0]];
  241. }
  242. }];
  243. }
  244. }];
  245. }
  246. #pragma mark - Lazy Loading
  247. - (UILabel *)pictureNumberLabel {
  248. if (!_pictureNumberLabel) {
  249. _pictureNumberLabel = [[UILabel alloc]init];
  250. _pictureNumberLabel.backgroundColor = [UIColor clearColor];
  251. _pictureNumberLabel.textAlignment = NSTextAlignmentLeft;
  252. _pictureNumberLabel.font = [UIFont boldSystemFontOfSize:12];
  253. _pictureNumberLabel.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0];
  254. _pictureNumberLabel.backgroundColor = [UIColor whiteColor];
  255. _pictureNumberLabel.frame = CGRectMake(10, 3, kScreenWidth - 10, 15);
  256. }
  257. return _pictureNumberLabel;
  258. }
  259. @end