// // UploadCoachContractVC.m // LN_School // // Created by apple on 2017/10/22. // Copyright © 2017年 Danson. All rights reserved. // #import "UploadCoachContractVC.h" #import "CoachContractView.h" @interface UploadCoachContractVC () { UIView *addView; UIScrollView *mainScroll; NSMutableArray *imageViewArray; NSMutableArray *frameArray; } @end @implementation UploadCoachContractVC - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.view.height -= kNavOffSet; self.navigationItem.title = @"上传合同"; [self goBackByNavigation]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(0, 0, 43, 44); btn.titleLabel.font = [UIFont systemFontOfSize:17]; [btn setTitle:@"上传" forState:UIControlStateNormal]; [btn setTitleColor:defGreen forState:UIControlStateNormal]; [btn addTarget:self action:@selector(uploadCoachProtocol) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn]; //图册 mainScroll = [[UIScrollView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:mainScroll]; CGFloat w = (kSize.width - 30)/2.0; CGFloat h = (w - 20)*4/3.0 + 60; frameArray = [NSMutableArray arrayWithCapacity:21]; for (int i = 0; i < 21; i ++) { int row = i/2; int column = i%2; [frameArray addObject:NSStringFromCGRect(CGRectMake(10 + column*(w + 10), 10 + row*(h + 10), w, h))]; } NSArray *imgpathArray = [NSArray array]; if (_imgPaths.length > 0) { imgpathArray = [_imgPaths componentsSeparatedByString:@","]; } imageViewArray = [NSMutableArray array]; for (int i = 0; i < imgpathArray.count; i ++) { if (i < frameArray.count - 1) { CoachContractView *view = [[CoachContractView alloc] initWithFrame:CGRectFromString(frameArray[i])]; [view borderCornorRadios:4]; view.tag = i; view.superVC = self; view.isHavePhoto = YES; [view.imgView sd_setImageWithURL:[NSURL URLWithString:imgpathArray[i]]]; [view deleteSelfWithBlock:^(NSString *tagString) { NSInteger index = [tagString integerValue]; if (index < imageViewArray.count) { CoachContractView *view = imageViewArray[index]; [view removeFromSuperview]; [imageViewArray removeObjectAtIndex:index]; [self setUI]; } }]; [mainScroll addSubview:view]; [imageViewArray addObject:view]; } } addView = [[UIView alloc] initWithFrame:CGRectFromString(frameArray[imageViewArray.count])]; addView.backgroundColor = KBackGroundColor; [addView borderCornorRadios:4]; [mainScroll addSubview:addView]; // UIImageView *imageView = [[UIImageView alloc] initWithFrame:addView.bounds]; // imageView.contentMode = UIViewContentModeScaleToFill; // imageView.image = [UIImage imageNamed:@"add_someThing"]; // [addView addSubview:imageView]; btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = addView.bounds; [btn setImage:[UIImage imageNamed:@"add_someThing"] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(addCoachContract) forControlEvents:UIControlEventTouchUpInside]; [addView addSubview:btn]; [mainScroll setContentSize:CGSizeMake(kSize.width, addView.y + addView.height + 50)]; //提示 // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 30 + 2*h, kSize.width - 40, 40)]; // [label setText:@"注:合同最少一张,最多四张" Font:Font17 TextColor:[UIColor orangeColor]]; // label.numberOfLines = 0; // [self.view addSubview:label]; } - (void)addCoachContract { if (imageViewArray.count >= frameArray.count - 1) { ShowMsg(@"已达添加上限!"); return; } CoachContractView *view = [[CoachContractView alloc] initWithFrame:CGRectFromString(frameArray[imageViewArray.count])]; [view borderCornorRadios:4]; view.tag = imageViewArray.count; view.superVC = self; view.isHavePhoto = NO; [view deleteSelfWithBlock:^(NSString *tagString) { NSInteger index = [tagString integerValue]; if (index < imageViewArray.count) { CoachContractView *view = imageViewArray[index]; [view removeFromSuperview]; [imageViewArray removeObjectAtIndex:index]; [self setUI]; } }]; [mainScroll addSubview:view]; [imageViewArray addObject:view]; addView.frame = CGRectFromString(frameArray[imageViewArray.count]); [mainScroll setContentSize:CGSizeMake(kSize.width, addView.y + addView.height + 50)]; } - (void)setUI { for (int i = 0; i < imageViewArray.count; i ++) { CoachContractView *view = imageViewArray[i]; view.frame = CGRectFromString(frameArray[i]); view.tag = i; } addView.frame = CGRectFromString(frameArray[imageViewArray.count]); [mainScroll setContentSize:CGSizeMake(kSize.width, addView.y + addView.height + 50)]; } - (void)uploadCoachProtocol { BOOL isHavePhoto = NO; for (CoachContractView *view in imageViewArray) { if (view.isHavePhoto) { isHavePhoto = YES; } } if (!isHavePhoto) { ShowMsg(@"合同最少一张,请拍照后上传!"); return; } //判断网络是否连接 if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSString *allPhotoString = @""; for (CoachContractView *view in imageViewArray) { if (view.isHavePhoto) { //[view.imgView.image originImageScaleToSize:CGSizeMake(640, 480)] UIImage *image = view.imgView.image; //处理照片 NSData *data = UIImageJPEGRepresentation(image,1.0); if (data.length > 50*1024) { //如果图片大于50kb 就压缩 data = UIImageJPEGRepresentation(image,0.5); } NSString *photoString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; allPhotoString = [allPhotoString stringByAppendingString:[NSString stringWithFormat:@"%@,",photoString]]; // NSLog(@"原图---->>>>%f\n处理后---->>>>%f",[UIImageJPEGRepresentation(image,1.0) length]/1024.0,data.length/1024.0); } } if (allPhotoString.length < 1) { ShowMsg(@"合同最少一张,请拍照后上传!"); return; } allPhotoString = [allPhotoString substringToIndex:allPhotoString.length - 1]; NSMutableDictionary * dic = [NSMutableDictionary new]; [dic setValue:_coachID forKey:@"coachId"]; [dic setValue:allPhotoString forKey:@"content"]; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:@"uploadCoachProtocol" dictionary:dic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { ShowMsg(@"上传失败,请重试!"); return; } ShowMsg(root[@"msg"]); if ([root[@"code"] integerValue] == 1) { return; } if (self.block) { self.block(); } [self.navigationController popViewControllerAnimated:YES]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end