UploadCoachContractVC.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // UploadCoachContractVC.m
  3. // LN_School
  4. //
  5. // Created by apple on 2017/10/22.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "UploadCoachContractVC.h"
  9. #import "CoachContractView.h"
  10. @interface UploadCoachContractVC ()
  11. {
  12. UIView *addView;
  13. UIScrollView *mainScroll;
  14. NSMutableArray<CoachContractView *> *imageViewArray;
  15. NSMutableArray *frameArray;
  16. }
  17. @end
  18. @implementation UploadCoachContractVC
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. self.view.height -= kNavOffSet;
  23. self.navigationItem.title = @"上传合同";
  24. [self goBackByNavigation];
  25. UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
  26. btn.frame = CGRectMake(0, 0, 43, 44);
  27. btn.titleLabel.font = [UIFont systemFontOfSize:17];
  28. [btn setTitle:@"上传" forState:UIControlStateNormal];
  29. [btn setTitleColor:defGreen forState:UIControlStateNormal];
  30. [btn addTarget:self action:@selector(uploadCoachProtocol) forControlEvents:UIControlEventTouchUpInside];
  31. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  32. //图册
  33. mainScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];
  34. [self.view addSubview:mainScroll];
  35. CGFloat w = (kSize.width - 30)/2.0;
  36. CGFloat h = (w - 20)*4/3.0 + 60;
  37. frameArray = [NSMutableArray arrayWithCapacity:21];
  38. for (int i = 0; i < 21; i ++) {
  39. int row = i/2;
  40. int column = i%2;
  41. [frameArray addObject:NSStringFromCGRect(CGRectMake(10 + column*(w + 10), 10 + row*(h + 10), w, h))];
  42. }
  43. NSArray *imgpathArray = [NSArray array];
  44. if (_imgPaths.length > 0) {
  45. imgpathArray = [_imgPaths componentsSeparatedByString:@","];
  46. }
  47. imageViewArray = [NSMutableArray array];
  48. for (int i = 0; i < imgpathArray.count; i ++) {
  49. if (i < frameArray.count - 1) {
  50. CoachContractView *view = [[CoachContractView alloc] initWithFrame:CGRectFromString(frameArray[i])];
  51. [view borderCornorRadios:4];
  52. view.tag = i;
  53. view.superVC = self;
  54. view.isHavePhoto = YES;
  55. [view.imgView sd_setImageWithURL:[NSURL URLWithString:imgpathArray[i]]];
  56. [view deleteSelfWithBlock:^(NSString *tagString) {
  57. NSInteger index = [tagString integerValue];
  58. if (index < imageViewArray.count) {
  59. CoachContractView *view = imageViewArray[index];
  60. [view removeFromSuperview];
  61. [imageViewArray removeObjectAtIndex:index];
  62. [self setUI];
  63. }
  64. }];
  65. [mainScroll addSubview:view];
  66. [imageViewArray addObject:view];
  67. }
  68. }
  69. addView = [[UIView alloc] initWithFrame:CGRectFromString(frameArray[imageViewArray.count])];
  70. addView.backgroundColor = KBackGroundColor;
  71. [addView borderCornorRadios:4];
  72. [mainScroll addSubview:addView];
  73. // UIImageView *imageView = [[UIImageView alloc] initWithFrame:addView.bounds];
  74. // imageView.contentMode = UIViewContentModeScaleToFill;
  75. // imageView.image = [UIImage imageNamed:@"add_someThing"];
  76. // [addView addSubview:imageView];
  77. btn = [UIButton buttonWithType:UIButtonTypeCustom];
  78. btn.frame = addView.bounds;
  79. [btn setImage:[UIImage imageNamed:@"add_someThing"] forState:UIControlStateNormal];
  80. [btn addTarget:self action:@selector(addCoachContract) forControlEvents:UIControlEventTouchUpInside];
  81. [addView addSubview:btn];
  82. [mainScroll setContentSize:CGSizeMake(kSize.width, addView.y + addView.height + 50)];
  83. //提示
  84. // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30 + 2*h, kSize.width - 40, 40)];
  85. // [label setText:@"注:合同最少一张,最多四张" Font:Font17 TextColor:[UIColor orangeColor]];
  86. // label.numberOfLines = 0;
  87. // [self.view addSubview:label];
  88. }
  89. - (void)addCoachContract {
  90. if (imageViewArray.count >= frameArray.count - 1) {
  91. ShowMsg(@"已达添加上限!");
  92. return;
  93. }
  94. CoachContractView *view = [[CoachContractView alloc] initWithFrame:CGRectFromString(frameArray[imageViewArray.count])];
  95. [view borderCornorRadios:4];
  96. view.tag = imageViewArray.count;
  97. view.superVC = self;
  98. view.isHavePhoto = NO;
  99. [view deleteSelfWithBlock:^(NSString *tagString) {
  100. NSInteger index = [tagString integerValue];
  101. if (index < imageViewArray.count) {
  102. CoachContractView *view = imageViewArray[index];
  103. [view removeFromSuperview];
  104. [imageViewArray removeObjectAtIndex:index];
  105. [self setUI];
  106. }
  107. }];
  108. [mainScroll addSubview:view];
  109. [imageViewArray addObject:view];
  110. addView.frame = CGRectFromString(frameArray[imageViewArray.count]);
  111. [mainScroll setContentSize:CGSizeMake(kSize.width, addView.y + addView.height + 50)];
  112. }
  113. - (void)setUI {
  114. for (int i = 0; i < imageViewArray.count; i ++) {
  115. CoachContractView *view = imageViewArray[i];
  116. view.frame = CGRectFromString(frameArray[i]);
  117. view.tag = i;
  118. }
  119. addView.frame = CGRectFromString(frameArray[imageViewArray.count]);
  120. [mainScroll setContentSize:CGSizeMake(kSize.width, addView.y + addView.height + 50)];
  121. }
  122. - (void)uploadCoachProtocol {
  123. BOOL isHavePhoto = NO;
  124. for (CoachContractView *view in imageViewArray) {
  125. if (view.isHavePhoto) {
  126. isHavePhoto = YES;
  127. }
  128. }
  129. if (!isHavePhoto) {
  130. ShowMsg(@"合同最少一张,请拍照后上传!");
  131. return;
  132. }
  133. //判断网络是否连接
  134. if (![NetManager connectedToNetWork]) {
  135. showMsgUnconnect();
  136. return;
  137. }
  138. NSString *allPhotoString = @"";
  139. for (CoachContractView *view in imageViewArray) {
  140. if (view.isHavePhoto) {
  141. //[view.imgView.image originImageScaleToSize:CGSizeMake(640, 480)]
  142. UIImage *image = view.imgView.image;
  143. //处理照片
  144. NSData *data = UIImageJPEGRepresentation(image,1.0);
  145. if (data.length > 50*1024) {
  146. //如果图片大于50kb 就压缩
  147. data = UIImageJPEGRepresentation(image,0.5);
  148. }
  149. NSString *photoString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  150. allPhotoString = [allPhotoString stringByAppendingString:[NSString stringWithFormat:@"%@,",photoString]];
  151. // NSLog(@"原图---->>>>%f\n处理后---->>>>%f",[UIImageJPEGRepresentation(image,1.0) length]/1024.0,data.length/1024.0);
  152. }
  153. }
  154. if (allPhotoString.length < 1) {
  155. ShowMsg(@"合同最少一张,请拍照后上传!");
  156. return;
  157. }
  158. allPhotoString = [allPhotoString substringToIndex:allPhotoString.length - 1];
  159. NSMutableDictionary * dic = [NSMutableDictionary new];
  160. [dic setValue:_coachID forKey:@"coachId"];
  161. [dic setValue:allPhotoString forKey:@"content"];
  162. [MBProgressHUD showLoadToView:self.view];
  163. [NetManager requestAnythingWithURL:@"uploadCoachProtocol" dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
  164. [MBProgressHUD hideHUDForView:self.view];
  165. if (!root) {
  166. ShowMsg(@"上传失败,请重试!");
  167. return;
  168. }
  169. ShowMsg(root[@"msg"]);
  170. if ([root[@"code"] integerValue] == 1) {
  171. return;
  172. }
  173. if (self.block) {
  174. self.block();
  175. }
  176. [self.navigationController popViewControllerAnimated:YES];
  177. }];
  178. }
  179. - (void)didReceiveMemoryWarning {
  180. [super didReceiveMemoryWarning];
  181. }
  182. @end