UploadCarCredentials.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // UploadCarCredentials.m
  3. // LN_School
  4. //
  5. // Created by apple on 2017/10/22.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "UploadCarCredentials.h"
  9. #import "PureCamera.h"
  10. #import "HKClipperHelper.h"
  11. #import "SkimViewController.h"
  12. @interface UploadCarCredentials ()<UIActionSheetDelegate>
  13. {
  14. NSMutableArray *imgViewArray;
  15. int type; //1、车辆照片2、行驶证(正本)3、行驶证(副本)4、综合性能检测报告
  16. }
  17. @end
  18. @implementation UploadCarCredentials
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.view.backgroundColor = [UIColor whiteColor];
  22. NSArray *titleArray = @[@"车辆照片",@"综合性能检测报告",@"行驶证(正本)",@"行驶证(副本)"];
  23. imgViewArray = [NSMutableArray arrayWithCapacity:titleArray.count];
  24. CGFloat h = (self.view.height - kNavOffSet - 40 - 40)/2.0 - (_isPermission ? 0 : 30);
  25. CGFloat w = (kSize.width - 40)/2.0;
  26. for (int i = 0; i < titleArray.count; i ++) {
  27. int row = i/2;
  28. int column = i%2;
  29. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + column*w, 10 + row*h, w, h)];
  30. [view borderColor:[UIColor grayColor] width:0.5 cornorRadios:3];
  31. [self.view addSubview:view];
  32. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, w, 30)];
  33. [label setText:titleArray[i] Font:Font18 TextColor:KTitleColor Alignment:NSTextAlignmentCenter];
  34. [view addSubview:label];
  35. UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, w - 20, h - 90)];
  36. [imageView borderColor:[UIColor grayColor] width:0.5 cornorRadios:3];
  37. imageView.contentMode = UIViewContentModeScaleAspectFit;
  38. NSString *imgPath = _imgPathArray[i];
  39. if (imgPath.length > 0) {
  40. [imageView sd_setImageWithURL:[NSURL URLWithString:imgPath] placeholderImage:[UIImage imageNamed:@"NOImg"]];
  41. }else {
  42. imageView.image = [UIImage imageNamed:@"NOImg"];
  43. }
  44. imageView.tag = i + 1;
  45. [view addSubview:imageView];
  46. [imgViewArray addObject:imageView];
  47. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
  48. imageView.userInteractionEnabled = YES;
  49. [imageView addGestureRecognizer:tap];
  50. if (!_isPermission) {
  51. continue;
  52. }
  53. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(15, h - 45, w - 30, 30)];
  54. btn.backgroundColor = defGreen;
  55. [btn setTitle:@"上传/修改" textColor:KBackGroundColor font:Font17 fotState:UIControlStateNormal];
  56. [btn borderCornorRadios:3];
  57. [btn target:self Tag:i+1];
  58. [view addSubview:btn];
  59. }
  60. }
  61. -(void)tapClick:(UIGestureRecognizer *)gesture{
  62. SkimViewController *svc = [[SkimViewController alloc] initWithImageNames:_imgPathArray index:gesture.view.tag - 1];
  63. [self.navigationController pushViewController:svc animated:NO];
  64. //告诉应用程序状态栏需要刷新(9.0以后不用管)
  65. [self setNeedsStatusBarAppearanceUpdate];
  66. }
  67. - (void)btnClick:(UIButton *)sender {
  68. type = (int)sender.tag;
  69. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相册", nil];
  70. [sheet showInView:self.view];
  71. }
  72. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  73. if (buttonIndex == 0) {//拍照
  74. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  75. myDelegate.takePhotoAllRect = YES;
  76. PureCamera *homec = [[PureCamera alloc] init];
  77. homec.fininshcapture = ^(UIImage *photo) {
  78. //如果拍照过程取消 要另外处理
  79. myDelegate.takePhotoAllRect = NO;
  80. if (photo) {
  81. [self uploadCarImgWithImage:photo];
  82. }
  83. };
  84. [self presentViewController:homec
  85. animated:NO
  86. completion:nil];
  87. } else {
  88. NSLog(@"相机调用失败");
  89. }
  90. }else if (buttonIndex == 1){//相册
  91. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
  92. [HKClipperHelper shareManager].nav = self.navigationController;
  93. [HKClipperHelper shareManager].clippedImgSize = CGSizeMake(4, 5);
  94. [HKClipperHelper shareManager].clipperType = ClipperTypeImgMove;
  95. [HKClipperHelper shareManager].systemEditing = NO;
  96. [HKClipperHelper shareManager].isSystemType = NO;
  97. [HKClipperHelper shareManager].clippedImageHandler = ^(UIImage *photo) {
  98. [self uploadCarImgWithImage:photo];
  99. };
  100. [[HKClipperHelper shareManager] photoWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
  101. }else{
  102. NSLog(@"相册调用失败");
  103. }
  104. }
  105. }
  106. //上传与修改照片
  107. - (void)uploadCarImgWithImage:(UIImage *)img{
  108. //判断网络是否连接
  109. if (![NetManager connectedToNetWork]) {
  110. showMsgUnconnect();
  111. return;
  112. }
  113. NSData *data = UIImageJPEGRepresentation(img,1.0);
  114. if (data.length > 50*1024) {
  115. //如果图片大于50kb 就压缩
  116. data = UIImageJPEGRepresentation(img,0.5);
  117. }
  118. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  119. NSMutableDictionary * mdic = [NSMutableDictionary new];
  120. [mdic setValue:_carID forKey:@"carId"];
  121. [mdic setValue:[NSString stringWithFormat:@"%d",type] forKey:@"type"];
  122. [mdic setValue:imgString forKey:@"content"];
  123. NSString *method = @"uploadCarImg";
  124. [MBProgressHUD showLoadToView:self.view];
  125. [NetManager requestAnythingWithURL:method dictionary:mdic dataArray:nil completion:^(NSDictionary *root) {
  126. [MBProgressHUD hideHUDForView:self.view];
  127. if (!root) {
  128. ShowMsg(@"照片上传失败,请重试");
  129. return;
  130. }
  131. ShowMsg(root[@"msg"]);
  132. if ([root[@"code"] integerValue] == 1) {
  133. return;
  134. }
  135. UIImageView *imageView = imgViewArray[type - 1];
  136. imageView.image = img;
  137. NSMutableArray *array = [NSMutableArray arrayWithArray:_imgPathArray];
  138. [array replaceObjectAtIndex:type - 1 withObject:root[@"body"]];
  139. _imgPathArray = [NSArray arrayWithArray:array];
  140. }];
  141. }
  142. - (void)didReceiveMemoryWarning {
  143. [super didReceiveMemoryWarning];
  144. }
  145. @end