// // UploadCarCredentials.m // LN_School // // Created by apple on 2017/10/22. // Copyright © 2017年 Danson. All rights reserved. // #import "UploadCarCredentials.h" #import "PureCamera.h" #import "HKClipperHelper.h" #import "SkimViewController.h" @interface UploadCarCredentials () { NSMutableArray *imgViewArray; int type; //1、车辆照片2、行驶证(正本)3、行驶证(副本)4、综合性能检测报告 } @end @implementation UploadCarCredentials - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; NSArray *titleArray = @[@"车辆照片",@"综合性能检测报告",@"行驶证(正本)",@"行驶证(副本)"]; imgViewArray = [NSMutableArray arrayWithCapacity:titleArray.count]; CGFloat h = (self.view.height - kNavOffSet - 40 - 40)/2.0 - (_isPermission ? 0 : 30); CGFloat w = (kSize.width - 40)/2.0; for (int i = 0; i < titleArray.count; i ++) { int row = i/2; int column = i%2; UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + column*w, 10 + row*h, w, h)]; [view borderColor:[UIColor grayColor] width:0.5 cornorRadios:3]; [self.view addSubview:view]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, w, 30)]; [label setText:titleArray[i] Font:Font18 TextColor:KTitleColor Alignment:NSTextAlignmentCenter]; [view addSubview:label]; UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, w - 20, h - 90)]; [imageView borderColor:[UIColor grayColor] width:0.5 cornorRadios:3]; imageView.contentMode = UIViewContentModeScaleAspectFit; NSString *imgPath = _imgPathArray[i]; if (imgPath.length > 0) { [imageView sd_setImageWithURL:[NSURL URLWithString:imgPath] placeholderImage:[UIImage imageNamed:@"NOImg"]]; }else { imageView.image = [UIImage imageNamed:@"NOImg"]; } imageView.tag = i + 1; [view addSubview:imageView]; [imgViewArray addObject:imageView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)]; imageView.userInteractionEnabled = YES; [imageView addGestureRecognizer:tap]; if (!_isPermission) { continue; } UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(15, h - 45, w - 30, 30)]; btn.backgroundColor = defGreen; [btn setTitle:@"上传/修改" textColor:KBackGroundColor font:Font17 fotState:UIControlStateNormal]; [btn borderCornorRadios:3]; [btn target:self Tag:i+1]; [view addSubview:btn]; } } -(void)tapClick:(UIGestureRecognizer *)gesture{ SkimViewController *svc = [[SkimViewController alloc] initWithImageNames:_imgPathArray index:gesture.view.tag - 1]; [self.navigationController pushViewController:svc animated:NO]; //告诉应用程序状态栏需要刷新(9.0以后不用管) [self setNeedsStatusBarAppearanceUpdate]; } - (void)btnClick:(UIButton *)sender { type = (int)sender.tag; UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil]; [sheet showInView:self.view]; } -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) {//拍照 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { myDelegate.takePhotoAllRect = YES; PureCamera *homec = [[PureCamera alloc] init]; homec.fininshcapture = ^(UIImage *photo) { //如果拍照过程取消 要另外处理 myDelegate.takePhotoAllRect = NO; if (photo) { [self uploadCarImgWithImage:photo]; } }; [self presentViewController:homec animated:NO completion:nil]; } else { NSLog(@"相机调用失败"); } }else if (buttonIndex == 1){//相册 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { [HKClipperHelper shareManager].nav = self.navigationController; [HKClipperHelper shareManager].clippedImgSize = CGSizeMake(4, 5); [HKClipperHelper shareManager].clipperType = ClipperTypeImgMove; [HKClipperHelper shareManager].systemEditing = NO; [HKClipperHelper shareManager].isSystemType = NO; [HKClipperHelper shareManager].clippedImageHandler = ^(UIImage *photo) { [self uploadCarImgWithImage:photo]; }; [[HKClipperHelper shareManager] photoWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; }else{ NSLog(@"相册调用失败"); } } } //上传与修改照片 - (void)uploadCarImgWithImage:(UIImage *)img{ //判断网络是否连接 if (![NetManager connectedToNetWork]) { showMsgUnconnect(); return; } NSData *data = UIImageJPEGRepresentation(img,1.0); if (data.length > 50*1024) { //如果图片大于50kb 就压缩 data = UIImageJPEGRepresentation(img,0.5); } NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; NSMutableDictionary * mdic = [NSMutableDictionary new]; [mdic setValue:_carID forKey:@"carId"]; [mdic setValue:[NSString stringWithFormat:@"%d",type] forKey:@"type"]; [mdic setValue:imgString forKey:@"content"]; NSString *method = @"uploadCarImg"; [MBProgressHUD showLoadToView:self.view]; [NetManager requestAnythingWithURL:method dictionary:mdic dataArray:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; if (!root) { ShowMsg(@"照片上传失败,请重试"); return; } ShowMsg(root[@"msg"]); if ([root[@"code"] integerValue] == 1) { return; } UIImageView *imageView = imgViewArray[type - 1]; imageView.image = img; NSMutableArray *array = [NSMutableArray arrayWithArray:_imgPathArray]; [array replaceObjectAtIndex:type - 1 withObject:root[@"body"]]; _imgPathArray = [NSArray arrayWithArray:array]; }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end