一、单张图片上传
1.首先,我们需要声明相应的属性以及代理:UIImagePickerControllerDelegate,UIActionSheetDelegate,
UIImage* compressedImage;//压缩后的图片,NSString* filePath;//本地文件路径,
NSData* fileData;//要上传的图片数据(上传图片,都是上传的数据流)
2.通过方法,触发,来选择选择图片上传还是拍照上传,以及选择照片之后进行的压缩图片,转换数据等操作
1 - (IBAction)tapForTakePhoto:(UITapGestureRecognizer *)sender { 2 3 UIActionSheet* actionSheet = [[UIActionSheet alloc]initWithTitle:@"上传照片" delegate:self cancelButtonTitle:@"取消上传" destructiveButtonTitle:nil otherButtonTitles:@"拍照上传",@"从相册上传", nil]; 4 actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 5 actionSheet.delegate = self; 6 [actionSheet showInView:self.view]; 7 8 } 9 10 11 12 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 13 14 //拍照 15 if (buttonIndex == 0) { 16 UIImagePickerController* imagePiker = [[UIImagePickerController alloc]init]; 17 imagePiker.sourceType = UIImagePickerControllerSourceTypeCamera; 18 imagePiker.delegate = self; 19 imagePiker.allowsEditing = NO; 20 [self presentViewController:imagePiker animated:YES completion:nil]; 21 22 } 23 //相册 24 else if(buttonIndex == 1){ 25 UIImagePickerController* imagePiker = [[UIImagePickerController alloc]init]; 26 imagePiker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 27 imagePiker.delegate = self; 28 imagePiker.allowsEditing = NO; 29 [self presentViewController:imagePiker animated:YES completion:nil]; 30 31 } 32 else{ 33 34 } 35 36 } 37 38 - (void)imagePickerController: (UIImagePickerController *)picker 39 didFinishPickingMediaWithInfo: (NSDictionary *)info 40 { 41 self.pickedImage.hidden = NO; 42 self.cameraView.hidden = YES; 43 44 //获得原始的图片 45 UIImage* image = [info objectForKey: @"UIImagePickerControllerOriginalImage"]; 46 47 UIImage* newImage = [MSUtils scaleImage:image scaleFactor:0.2]; 48 49 self.pickedImage.image = newImage; 50 51 self.compressedImage = newImage; 52 53 self.fileData = UIImageJPEGRepresentation(newImage, 0.5); 54 55 [self dismissViewControllerAnimated:YES completion:nil]; 56 }
3.图片上传问题,对于图片上传,如果使用的是AFN,那么里面包含了具体的使用方法,当然很多都是自己封装的方法,
网络请求:
1 //上传装车照片 2 -(void)sendRequestForSubmitLoad{ 3 MSRequestPaket * requestPaket = [[MSRequestPaket alloc]initWithRequestId:request_submitLoad]; 4 [requestPaket.parametersDic setObject:_carId forKey:@"carId"]; 5 [requestPaket.parametersDic setObject:_orderId forKey:@"orderId"]; 6 [requestPaket.parametersDic setObject:_traceStatus forKey:@"traceStatus"]; 7 [requestPaket.parametersDic setObject:_isAdditional forKey:@"isAdditional"]; 8 //照片名字 carId+orderId+traceStatus.jpg 9 NSString* fileName = [NSString stringWithFormat:@"%@_%@_%@.jpg",_carId,_orderId,_traceStatus]; 10 [[MSAFClient sharedClient]uploadFileWithData:_fileData andFileName:fileName andParameters:requestPaket.parametersDic Delegate:self]; 11 }
这个请求方法内部的执行:
1 //上传文件(data) 2 -(void)uploadFileWithData:(NSData*)data andFileName:(NSString*)fileName andParameters:(NSDictionary*)parameters Delegate:(id<MSAFClientDelegate>)delegate{ 3 4 NSURL* url = [[NSURL alloc]initWithString:MSDefaultBaseURL]; 5 6 AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:url]; 7 8 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 9 10 // manager.requestSerializer.stringEncoding = encodingType; 11 // manager.responseSerializer.stringEncoding = encodingType; 12 13 NSMutableURLRequest* mutableRequest = [[AFHTTPRequestSerializer serializer]requestWithMethod:@"GET" URLString:uploadPath parameters:parameters error:nil]; 14 15 //解析出文件名 16 NSString* name = [fileName stringByDeletingPathExtension]; 17 18 [manager POST:mutableRequest.URL.relativeString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 19 20 [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"multipart/form-data"]; 21 22 } success:^(AFHTTPRequestOperation *operation, id responseObject) { 23 //成功回调 24 DLog(@"upload success=====%@",operation.responseString); 25 [delegate UploadCompeleted:nil]; 26 27 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 28 29 //失败回调 30 DLog(@"upload failed=====%@",[error localizedDescription]); 31 DLog(@"------%@",[error.userInfo objectForKey:@"NSLocalizedRecoverySuggestion"]); 32 [delegate UploadFail:[error localizedDescription]]; 33 34 }]; 35 36 }
其他的就是网络请求的成功和失败回调了,这个就不在说明.
二、多张图片上传
1.对于多图片上传问题,我们可能有各种需求,就以我遇到的需求来说明:首先,我们需要请求数据,获取上传图片的数组,如果有网络图片,
那么我们就要先 显示网络图片,然后在原有的基础上进行增加和删除操作。所以,我们要先声明相应的属性:
/**
* 声明两个数组来接收上面传下来的路径数组
*/
@property (nonatomic, strong) NSMutableArray *feePhotoArray;
@property (nonatomic, strong) NSMutableArray *fdPhtotArray;
如果该界面不止一个地方要上传图片,那么需要声明一个值来区分属于哪个地方
@property (nonatomic, assign) NSInteger TYPETAG;
2.其他的操作和单张图片上传十分相似,不同的就是网络请求时,一个传的的单个数据,一个传的是数组,并且对于多张图片上传来说,
我们可以有两种方法,一种是一张一张的上传,利用一个循环来进行请求,另一种就是上传数组了,从性能来看数组操作要好些,
具体的就直接上代码了,其中有的是自己封装的方法,后面会将内部方法贴出:
1 #import "addPhotoViewController.h" 2 #import "CollectionViewCell.h" 3 #import "UIImageView+WebCache.h" 4 5 #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width 6 #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height 7 8 @interface addPhotoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,MSAFClientDelegate,UploadMorePhoteDelegate> 9 { 10 // UIImageView *PcameImage; 11 // UILabel *Plabel; 12 NSData *imageData; 13 NSString *fileName; 14 NSInteger TIMES; 15 NSInteger SENDER; 16 } 17 @property (strong, nonatomic) IBOutlet UICollectionView *myCollectionView; 18 @property (weak, nonatomic) IBOutlet UIButton *determineBtn; 19 20 /** 21 * 声明一个可变数组来存储全部照片 22 */ 23 @property (nonatomic, strong) NSMutableArray *savePhotosArray; 24 /** 25 * 存储删除\添加的图片的数组 26 */ 27 @property (nonatomic, strong) NSMutableArray *deleteImageArray; 28 @property (nonatomic, strong) NSMutableArray *addImageArray; 29 //@property (nonatomic, strong) NSMutableArray *addImageArrayWithImage; 30 31 @property (strong,nonatomic)UIImage* compressedImage;//压缩后的图片 32 33 @property (copy,nonatomic)NSString* filePath;//本地文件路径 34 //@property (strong,nonatomic)NSData* fileData;//要上传的图片数据 35 /** 36 * itemWH item宽高 margin 边缘 37 */ 38 @property (nonatomic, assign) CGFloat itemWH; 39 @property (nonatomic, assign) CGFloat margin; 40 41 @property (nonatomic, strong) CollectionViewCell *collectionViewCell; 42 /** 43 * 底图 44 */ 45 @property (nonatomic, copy) UIView *backView; 46 47 @property (strong,nonatomic)MSAlertView* alertView; 48 49 //定义一个记录回调的字段---是否刷新整个页面 50 @property (nonatomic, assign) BOOL onlyRefImageCount; 51 52 @property (nonatomic, assign) BOOL RefDataYseOrNo; 53 54 @end 55 56 @implementation addPhotoViewController 57 58 - (void)viewDidLoad 59 { 60 [super viewDidLoad]; 61 [self setNavigationbarStyle]; 62 63 _deleteImageArray = [NSMutableArray array]; 64 _addImageArray = [NSMutableArray array]; 65 // _addImageArrayWithImage = [NSMutableArray array]; 66 self.alertView = [MSAlertView sharedAlertView]; 67 [self prepareData]; 68 TIMES = 0; 69 } 70 -(void)prepareData 71 { 72 if (_feePhotoArray.count>0) { 73 NSMutableArray *array = [NSMutableArray array]; 74 for (NSDictionary *dict in _feePhotoArray) 75 { 76 [array addObject:dict[@"filepath"]]; 77 self.savePhotosArray = [NSMutableArray array]; 78 [self.savePhotosArray addObjectsFromArray:array]; 79 _TYPETAG = 1; 80 } 81 } 82 else if (_fdPhtotArray.count > 0) 83 { 84 NSMutableArray *array = [NSMutableArray array]; 85 for (NSDictionary *dic in _fdPhtotArray) 86 { 87 [array addObject:dic[@"filepath"]]; 88 self.savePhotosArray = [NSMutableArray array]; 89 [self.savePhotosArray addObjectsFromArray:array]; 90 _TYPETAG = 2; 91 } 92 }else 93 { 94 } 95 96 } 97 -(void)viewWillAppear:(BOOL)animated 98 { 99 [super viewWillAppear:animated]; 100 [self configUI]; 101 } 102 -(void)setNavigationbarStyle{ 103 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[MSUtils setScaleOfImage:@"back.png" AndScale:2.0] style:UIBarButtonItemStyleBordered target:self action:@selector(back)]; 104 [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]]; 105 } 106 -(void)back 107 { 108 self.RefDataYseOrNo = NO; 109 if ([self.myUpLoadDelegate respondsToSelector:@selector(NoUploadPhoto:)]) { 110 [self.myUpLoadDelegate NoUploadPhoto:[NSString stringWithFormat:@"%d",self.RefDataYseOrNo]]; 111 } 112 [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 113 } 114 115 -(void)configUI 116 { 117 118 self.title = @"添加照片"; 119 120 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; 121 [flowLayout setItemSize:CGSizeMake((SCREEN_WIDTH - 10*5)/4.0, (SCREEN_WIDTH - 10*5)/4.0)]; 122 [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; 123 _margin = 10; 124 _itemWH = (self.view.frame.size.width - 5*_margin)/4; 125 126 self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-60-30-43-64) collectionViewLayout:flowLayout]; 127 self.myCollectionView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1]; 128 /** 129 * 设置是否可以多选, 默认为NO 130 * 131 * @return yes 132 */ 133 self.myCollectionView.allowsMultipleSelection = YES; 134 self.myCollectionView.showsVerticalScrollIndicator = YES; 135 self.myCollectionView.delegate = self; 136 self.myCollectionView.dataSource = self; 137 [self.view addSubview:self.myCollectionView]; 138 [self.myCollectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"]; 139 self.determineBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 140 self.determineBtn.frame = CGRectMake(20, self.myCollectionView.frame.size.height+self.myCollectionView.frame.origin.y+40, SCREEN_WIDTH - 40, 30); 141 [self.view addSubview:self.determineBtn]; 142 [self.myCollectionView reloadData]; 143 144 } 145 #pragma mark---- UICollectionViewDataSource && UICollectionViewDelegate ----- 146 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 147 { 148 return 1; 149 } 150 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 151 { 152 return self.savePhotosArray.count+1; 153 } 154 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 155 { 156 /** 157 * 重用cell 158 */ 159 CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath]; 160 /** 161 * 加载cell的内容 162 * 保证第一个为 添加照片 163 */ 164 if (indexPath.row == 0) { 165 166 cell.PcameImage.hidden = NO; 167 cell.imageView.image = nil; 168 cell.deleteBtn.hidden = YES; 169 cell.Plabel.hidden = NO; 170 }else 171 { //判断是那种图片,本地-网络 172 if (_feePhotoArray.count>0) 173 { //网络图片 174 if (indexPath.row<_feePhotoArray.count+1) 175 { 176 cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.savePhotosArray[indexPath.row-1]]]]]; 177 } 178 else//本地图片 179 { 180 cell.imageView.image = self.savePhotosArray[indexPath.row-1]; 181 } 182 }else if (_fdPhtotArray.count>0) 183 { 184 if (indexPath.row<_fdPhtotArray.count+1) 185 { 186 cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.savePhotosArray[indexPath.row-1]]]]]; 187 } 188 else 189 { 190 cell.imageView.image = self.savePhotosArray[indexPath.row-1]; 191 } 192 } 193 else 194 { 195 cell.imageView.image = self.savePhotosArray[indexPath.row-1]; 196 } 197 cell.Plabel.hidden = YES; 198 cell.PcameImage.hidden = YES; 199 cell.imageView.userInteractionEnabled = YES; 200 cell.deleteBtn.tag = indexPath.row+1000; 201 cell.deleteBtn.hidden = NO; 202 [cell.deleteBtn addTarget:self action:@selector(deleteImageView:) forControlEvents:UIControlEventTouchUpInside]; 203 204 } 205 return cell; 206 } 207 -(void)deleteImageView:(UIButton *)sender 208 { 209 SENDER = sender.tag; 210 UIAlertView* alertView = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"是否删除当前图片" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; 211 alertView.tag = 1002; 212 [alertView show]; 213 } 214 /** 215 * 用代码来 控制每个UICollectionViewCell 的大小 216 */ 217 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 218 { 219 CGFloat width = (SCREEN_WIDTH - 10*5)/4.0; 220 CGFloat height = width; 221 return CGSizeMake(width, height); 222 } 223 /** 224 * 定义边距 margin 225 */ 226 -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 227 { 228 return UIEdgeInsetsMake(10, 10, 10, 10); 229 } 230 /** 231 * 选择某个cell 232 */ 233 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 234 { 235 if (indexPath.row == 0) 236 { 237 UIActionSheet* actionSheet = [[UIActionSheet alloc]initWithTitle:@"上传照片" delegate:self cancelButtonTitle:@"取消上传" destructiveButtonTitle:nil otherButtonTitles:@"拍照上传",@"从相册上传", nil]; 238 actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 239 actionSheet.delegate = self; 240 [actionSheet showInView:self.view]; 241 }else 242 { 243 _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; 244 _backView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.95]; 245 [self.view addSubview:_backView]; 246 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH , SCREEN_HEIGHT -100-64)]; 247 /** 248 * 根据图片的下标来判断***如果网络,则刚进来必定在前面 249 */ 250 if (_feePhotoArray.count>0) 251 { //网络图片 252 if (indexPath.row<_feePhotoArray.count+1) 253 { 254 imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.savePhotosArray[indexPath.row-1]]]]]; 255 } 256 else//本地图片 257 { 258 imageView.image = self.savePhotosArray[indexPath.row-1]; 259 } 260 }else if (_fdPhtotArray.count>0) 261 { 262 if (indexPath.row<_fdPhtotArray.count+1) 263 { 264 imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.savePhotosArray[indexPath.row-1]]]]]; 265 } 266 else 267 { 268 imageView.image = self.savePhotosArray[indexPath.row-1]; 269 } 270 } 271 else 272 { 273 imageView.image = self.savePhotosArray[indexPath.row-1]; 274 } 275 [_backView addSubview:imageView]; 276 } 277 } 278 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 279 { 280 //拍照 281 if (buttonIndex == 0) { 282 if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 283 { 284 UIImagePickerController* imagePiker = [[UIImagePickerController alloc]init]; 285 imagePiker.sourceType = UIImagePickerControllerSourceTypeCamera; 286 imagePiker.delegate = self; 287 imagePiker.allowsEditing = NO; 288 [self presentViewController:imagePiker animated:YES completion:nil]; 289 } 290 else 291 { 292 [[[UIAlertView alloc] initWithTitle:@"警告" message:@"您的设备没有相机。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; 293 } 294 295 } 296 //相册 297 else if(buttonIndex == 1){ 298 UIImagePickerController* imagePiker = [[UIImagePickerController alloc]init]; 299 imagePiker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 300 imagePiker.delegate = self; 301 imagePiker.allowsEditing = NO; 302 [self presentViewController:imagePiker animated:YES completion:nil]; 303 } 304 305 } 306 - (void)imagePickerController: (UIImagePickerController *)picker 307 didFinishPickingMediaWithInfo: (NSDictionary *)info 308 { 309 //获得原始的图片 310 UIImage* image = [info objectForKey: @"UIImagePickerControllerOriginalImage"]; 311 312 // [_addImageArrayWithImage addObject:image]; 313 314 if (!self.savePhotosArray) { 315 self.savePhotosArray = [NSMutableArray array]; 316 } 317 [self.savePhotosArray addObject:image]; 318 319 UIImage* newImage = [MSUtils scaleImage:image scaleFactor:0.2]; 320 321 self.compressedImage = newImage; 322 323 324 NSData* fileData = UIImageJPEGRepresentation(newImage, 0.5); 325 [self.addImageArray addObject:fileData]; 326 327 [self dismissViewControllerAnimated:YES completion:nil]; 328 329 } 330 331 -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 332 { 333 [_backView removeFromSuperview]; 334 _backView = nil; 335 } 336 - (IBAction)determineBtnClick:(id)sender 337 { 338 [self sendRequestForUploadThInfoImages]; 339 } 340 341 342 #pragma mark ************ 网络请求 上传图片 ************** 343 -(void)sendRequestForUploadThInfoImages 344 { 345 MSRequestPaket * requestPaket = [[MSRequestPaket alloc]initWithRequestId:request_uploadThInfoImages]; 346 NSString * memberId = [[BSUserInfo defaultUserInfo] memberId]; 347 [requestPaket.parametersDic setObject:memberId forKey:@"customerId"]; 348 NSString * userId = [[BSUserInfo defaultUserInfo] userId]; 349 [requestPaket.parametersDic setObject:userId forKey:@"userId"]; 350 [requestPaket.parametersDic setObject:_wayBillId forKey:@"waybillId"]; 351 [requestPaket.parametersDic setObject:_loadingBilldId forKey:@"loadingBilldId"]; 352 353 //fee:费用图片 fd:返单图片 354 if (_TYPETAG == 1) { 355 [requestPaket.parametersDic setObject:@"fee" forKey:@"uploaded_file"]; 356 } 357 else if (_TYPETAG == 2) 358 { 359 [requestPaket.parametersDic setObject:@"fd" forKey:@"uploaded_file"]; 360 } 361 362 363 if (_deleteImageArray.count > 0) { 364 365 [self.alertView startAnimating:self WithText:@"正在提交信息..."]; 366 367 NSString *jsonString = [self changeJsonDateToJsonStringWithArray:_deleteImageArray]; 368 [requestPaket.parametersDic setObject:jsonString forKey:@"delImage"]; 369 370 if (self.addImageArray.count>0) { 371 for (int i = 0; i<self.addImageArray.count; i++) 372 { 373 fileName = [NSString stringWithFormat:@"%d.jpg",i+1]; 374 375 imageData = [self.addImageArray objectAtIndex:i]; 376 [[MSAFClient sharedClient]uploadFileWithData:imageData andFileName:fileName andParameters:requestPaket.parametersDic Delegate:self]; 377 TIMES++; 378 } 379 }else 380 { 381 [[MSAFClient sharedClient]RequestByGetWithPath:pathname Parameters:requestPaket.parametersDic Delegate:self SerialNum:1 IfUserCache:NO]; 382 } 383 }else 384 { 385 if (self.addImageArray.count>0) { 386 387 [self.alertView startAnimating:self WithText:@"正在提交信息..."]; 388 for (int i = 0; i<self.addImageArray.count; i++) 389 { 390 391 fileName = [NSString stringWithFormat:@"%d.jpg",i+1]; 392 imageData = [self.addImageArray objectAtIndex:i]; 393 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 394 NSString *docDir = [paths objectAtIndex:0]; 395 NSLog(@"docDirPath--------- %@",docDir); 396 397 [[MSAFClient sharedClient]uploadFileWithData:imageData andFileName:fileName andParameters:requestPaket.parametersDic Delegate:self]; 398 TIMES++; 399 } 400 401 }else{ 402 UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"请删除或添加图片"] delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil]; 403 [alertview show]; 404 } 405 406 } 407 408 } 409 #pragma mark -- 解析数据 转换为上传json数据 410 -(NSString *)changeJsonDateToJsonStringWithArray:(NSArray *)array 411 { 412 if ([NSJSONSerialization isValidJSONObject:array]) { 413 NSData * data = [NSJSONSerialization dataWithJSONObject:array options:0 error:nil]; 414 NSString * dataString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; 415 return dataString; 416 } 417 return @""; 418 } 419 420 -(void)UploadCompeleted:(NSString *)result{ 421 422 [self.alertView stopAnimating]; 423 if (TIMES == self.addImageArray.count) { 424 UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"操作成功"] delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil]; 425 alertview.tag = 1001; 426 [alertview show]; 427 TIMES--; 428 return; 429 } 430 DLog(@"上传成功"); 431 } 432 433 -(void)UploadFail:(NSString *)errorString{ 434 DLog(@"操作失败"); 435 [self.alertView stopAnimating]; 436 [MSUtils showAlertViewWithError:errorString]; 437 } 438 //成功回调 439 -(void)RequestSuccess:(NSDictionary*)responceDic SerialNum:(int)serialNum 440 { 441 DLog(@"-----------%@",responceDic); 442 //停止指示框 443 [self.alertView stopAnimating]; 444 NSString* result = [responceDic objectForKey:@"result"]; 445 //返回失败 446 if (![result isEqualToString:@"true"]) 447 { 448 NSString* error = [responceDic objectForKey:@"errorstr"]; 449 [MSUtils showAlertViewWithError:error]; 450 return; 451 } 452 if (serialNum == 1) 453 { 454 UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"删除成功"] delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil]; 455 alertview.tag = 1001; 456 [alertview show]; 457 458 } 459 } 460 //失败回调 461 -(void)RequsetFail:(NSString*)errorString SerialNum:(int)serialNum{ 462 463 //停止指示框 464 [self.alertView stopAnimating]; 465 NSString* error = [NSString stringWithFormat:@"请求数据失败:%@",errorString]; 466 [MSUtils showAlertViewWithError:error]; 467 } 468 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 469 { 470 if (alertView.tag == 1001) 471 { 472 self.onlyRefImageCount = NO; 473 if ([self.myUpLoadDelegate respondsToSelector:@selector(uploadMorePhoteCompeleted:)]) { 474 [self.myUpLoadDelegate uploadMorePhoteCompeleted:[NSString stringWithFormat:@"%d",self.onlyRefImageCount]]; 475 } 476 477 [self.navigationController dismissViewControllerAnimated:YES completion:^{ 478 }]; 479 } 480 if (alertView.tag == 1002) { 481 if (buttonIndex == 1) { 482 if (_TYPETAG == 1) 483 { 484 if (SENDER-1000-1<_feePhotoArray.count) 485 { 486 NSMutableDictionary * dictionary = [NSMutableDictionary dictionary]; 487 [dictionary setObject:self.savePhotosArray[SENDER-1000-1] forKey:@"filepath"]; 488 for (NSDictionary *dic in _feePhotoArray) 489 { 490 if ([dic[@"filepath"] isEqualToString:[NSString stringWithFormat:@"%@",self.savePhotosArray[SENDER-1000-1]]]) 491 { 492 [dictionary setValue:dic[@"fileId"] forKey:@"fileId"]; 493 } 494 } 495 [_deleteImageArray addObject:dictionary]; 496 [_feePhotoArray removeObjectAtIndex:SENDER-1000-1]; 497 } 498 else 499 { 500 [self.addImageArray removeObjectAtIndex:SENDER-1000-_feePhotoArray.count-1]; 501 // [self.addImageArrayWithImage removeObjectAtIndex:SENDER-1000-_feePhotoArray.count]; 502 } 503 504 [self.savePhotosArray removeObjectAtIndex:SENDER-1000-1]; 505 506 } 507 else if (_TYPETAG == 2) 508 { 509 /** 510 * 根据下标位置来判断 是网络图片还是本地图片 511 */ 512 if (SENDER-1000-1<_fdPhtotArray.count) 513 { 514 NSMutableDictionary * dictionary = [NSMutableDictionary dictionary]; 515 516 [dictionary setObject:self.savePhotosArray[SENDER-1000-1] forKey:@"filepath"]; 517 for (NSDictionary *dic in _fdPhtotArray) 518 { 519 if ([dic[@"filepath"] isEqualToString:[NSString stringWithFormat:@"%@",self.savePhotosArray[SENDER-1000-1]]]) 520 { 521 [dictionary setValue:dic[@"fileId"] forKey:@"fileId"]; 522 } 523 } 524 [_deleteImageArray addObject:dictionary]; 525 [_fdPhtotArray removeObjectAtIndex:SENDER-1000-1]; 526 NSLog(@"##############_deleteImageArray %@",_deleteImageArray); 527 } 528 else 529 { 530 [self.addImageArray removeObjectAtIndex:SENDER-1000-_fdPhtotArray.count-1]; 531 // [self.addImageArrayWithImage removeObjectAtIndex:SENDER-1000-_fdPhtotArray.count]; 532 } 533 [self.savePhotosArray removeObjectAtIndex:SENDER-1000-1]; 534 } 535 [self.myCollectionView reloadData]; 536 537 } 538 } 539 } 540 - (void)didReceiveMemoryWarning { 541 [super didReceiveMemoryWarning]; 542 // Dispose of any resources that can be recreated. 543 } 544 545 @end
我这里具体是使用一个collectionView来展示图片信息的,每个item就是一个图片,collectionViewCell的布局为:
1 #import <UIKit/UIKit.h> 2 3 //@protocol twoTapsDelegate <NSObject> 4 ///** 5 // * 放大视图 6 // */ 7 //-(void)amplificationForView; 8 // 9 //@end 10 11 @interface CollectionViewCell : UICollectionViewCell 12 13 @property (nonatomic, strong) UIImageView *imageView; 14 @property (nonatomic, strong) UIButton *deleteBtn; 15 @property (nonatomic, strong) UIImageView *PcameImage; 16 @property (nonatomic, strong) UILabel *Plabel; 17 //@property (nonatomic, assign) id<twoTapsDelegate>myTapDelegate; 18 19 @end 20 21 22 #import "CollectionViewCell.h" 23 24 @implementation CollectionViewCell 25 26 -(instancetype)initWithFrame:(CGRect)frame 27 { 28 self = [super initWithFrame:frame]; 29 if (self) { 30 [self createUI]; 31 } 32 return self; 33 } 34 -(void)createUI 35 { 36 self.backgroundColor = [UIColor whiteColor]; 37 _imageView = [[UIImageView alloc] init]; 38 _imageView.backgroundColor = [UIColor grayColor]; 39 _imageView.contentMode = UIViewContentModeScaleAspectFill; 40 [self addSubview:_imageView]; 41 _imageView.frame = self.bounds; 42 _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 43 _deleteBtn.frame = CGRectMake(self.frame.size.width - 20, 0, 20, 20); 44 _deleteBtn.layer.cornerRadius = 10; 45 [_deleteBtn setTitle:@"一" forState:UIControlStateNormal]; 46 [_deleteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 47 _deleteBtn.backgroundColor = [UIColor redColor]; 48 [self.imageView addSubview:_deleteBtn]; 49 _deleteBtn.enabled = YES; 50 _deleteBtn.hidden = YES; 51 self.clipsToBounds = YES; 52 _PcameImage = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width-41)/2, (self.frame.size.height-31-21)/2, 41, 31)]; 53 [_PcameImage setImage:[UIImage imageNamed:@"camera.png"]]; 54 _PcameImage.hidden = NO; 55 [_imageView addSubview:_PcameImage]; 56 _Plabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _PcameImage.frame.origin.y+_PcameImage.frame.size.height, self.frame.size.width, 21)]; 57 _Plabel.text = @"添加照片"; 58 _Plabel.font = [UIFont systemFontOfSize:14.0]; 59 _Plabel.textColor = [UIColor whiteColor]; 60 _Plabel.textAlignment = NSTextAlignmentCenter; 61 _Plabel.hidden = NO; 62 [_imageView addSubview:_Plabel]; 63 64 } 65 -(void)layoutSubviews 66 { 67 [super layoutSubviews]; 68 }
多图片上传的请求内部方法同样和一张的一样!效果图:
3.另外在其他的地方,我使用了数组的方法来上传图片,在这个页面中,我们是在添加图片的页面只做添加删除操作,不上传,
点击确定按钮确定后回到上个主页面,在主页面对返回的图片数组进行上传操作,具体代码(里面有很多项目内容,如果看的不舒服,就直接看与图片相关的就好):
(1)添加删除图片页面
1 #import <UIKit/UIKit.h> 2 3 @protocol backImgNameDelegate <NSObject> 4 5 -(void)backImageName:(NSDictionary *)dict; 6 7 @end 8 9 @interface addPhotoImgViewController : UIViewController 10 11 @property (nonatomic, strong) NSString *photoTypeStr;//哪个图片内容跳转 12 @property (nonatomic, strong) NSString *tapEnableStr;//点击是否可用 13 @property (nonatomic, strong) NSMutableArray *photoArray;//添加的图片的数组 14 @property (nonatomic, strong) NSString *imagePathStr;//总图片路径 15 @property (nonatomic, strong) NSString *photoPathStr;//图片路径 16 @property (nonatomic, strong) NSString *pathStr;//http://....... 17 18 @property (nonatomic, strong) NSString *shStatusStr;//记录审核字段 19 20 @property (nonatomic, assign) id<backImgNameDelegate>myDelegate; 21 22 @end
1 #import "addPhotoImgViewController.h" 2 #import "AddVehicleInfoViewController.h" 3 4 @interface addPhotoImgViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,MSAFClientDelegate> 5 { 6 NSMutableDictionary *dict; 7 } 8 @property (weak, nonatomic) IBOutlet UIImageView *photoImg; 9 @property (weak, nonatomic) IBOutlet UIImageView *cameraImg; 10 @property (weak, nonatomic) IBOutlet UILabel *titleLab; 11 12 @property (weak, nonatomic) IBOutlet UIButton *comfirBtn; 13 @property (nonatomic, strong) NSString *fileName; 14 15 @end 16 17 @implementation addPhotoImgViewController 18 19 - (void)viewDidLoad { 20 [super viewDidLoad]; 21 // Do any additional setup after loading the view from its nib. 22 self.title = @"拍照"; 23 self.titleLab.hidden = NO; 24 self.cameraImg.hidden = NO; 25 self.navigationController.navigationBar.translucent = NO; 26 [self setNavigationbarStyle]; 27 [self configPhotoImg]; 28 } 29 /** 30 * 进来时,如果有图片,则加载图片 31 */ 32 -(void)configPhotoImg 33 { 34 //显示查看网络图片 35 if (![self isBlankString:self.photoPathStr]) 36 { 37 if ([self.shStatusStr isEqualToString:SHSTATUS_Through])//审核通过 38 { 39 self.photoImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.imagePathStr]]]]; 40 self.titleLab.hidden = YES; 41 self.cameraImg.hidden = YES; 42 self.comfirBtn.hidden = YES; 43 self.title = @"查看照片"; 44 } 45 else 46 { 47 self.photoImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.imagePathStr]]]]; 48 self.titleLab.hidden = YES; 49 self.cameraImg.hidden = YES; 50 self.comfirBtn.hidden = NO; 51 self.title = @"查看照片"; 52 } 53 } 54 else 55 { 56 //本地未上传时 57 if (self.photoArray.count > 0) 58 { 59 for (NSMutableDictionary *dic in self.photoArray) 60 { 61 NSLog(@"++++++++ %@",dic[@"photoType"]); 62 if ([dic[@"photoType"] isEqualToString:self.photoTypeStr]) 63 { 64 self.photoImg.image = dic[@"image"]; 65 self.titleLab.hidden = YES; 66 self.cameraImg.hidden = YES; 67 self.comfirBtn.hidden = NO; 68 dict = dic; 69 return; 70 } 71 } 72 73 } 74 else 75 { 76 if ([self.shStatusStr isEqualToString:SHSTATUS_Through])//审核通过 77 { 78 self.titleLab.hidden = YES; 79 self.cameraImg.hidden = YES; 80 self.comfirBtn.hidden = YES; 81 } 82 else 83 { 84 self.titleLab.hidden = NO; 85 self.cameraImg.hidden = NO; 86 self.comfirBtn.hidden = NO; 87 } 88 89 } 90 } 91 } 92 //判断字符串是否为空 93 - (BOOL) isBlankString:(NSString *)string { 94 if ([string isEqualToString:@""] || [string isEqualToString:@" "]) { 95 return YES; 96 } 97 if (string == nil || string == NULL) { 98 return YES; 99 } 100 if ([string isKindOfClass:[NSNull class]]) { 101 return YES; 102 } 103 if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) { 104 return YES; 105 } 106 return NO; 107 } 108 -(void)setNavigationbarStyle{ 109 110 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[MSUtils setScaleOfImage:@"back.png" AndScale:2.0] style:UIBarButtonItemStyleBordered target:self action:@selector(backHome:)]; 111 [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]]; 112 } 113 -(void)backHome:(id)sender{ 114 [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 115 } 116 #pragma mark ----- 点击事件 ----- 117 - (IBAction)photoImgTap:(id)sender 118 { 119 //查看时,点击无效,添加时点击添加或拍照 120 if ([self.tapEnableStr isEqualToString:SHSTATUS_Through]) 121 { 122 self.photoImg.userInteractionEnabled = NO; 123 }else 124 { 125 UIActionSheet* actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择照片" delegate:self cancelButtonTitle:@"取消选择" destructiveButtonTitle:nil otherButtonTitles:@"拍照选取",@"从相册选取", nil]; 126 actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent; 127 actionSheet.delegate = self; 128 [actionSheet showInView:self.view]; 129 } 130 } 131 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{ 132 133 //拍照 134 if (buttonIndex == 0) { 135 UIImagePickerController* imagePiker = [[UIImagePickerController alloc]init]; 136 imagePiker.sourceType = UIImagePickerControllerSourceTypeCamera; 137 imagePiker.delegate = self; 138 imagePiker.allowsEditing = NO; 139 [self presentViewController:imagePiker animated:YES completion:nil]; 140 141 } 142 //相册 143 else if(buttonIndex == 1){ 144 UIImagePickerController* imagePiker = [[UIImagePickerController alloc]init]; 145 imagePiker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 146 imagePiker.delegate = self; 147 imagePiker.allowsEditing = NO; 148 [self presentViewController:imagePiker animated:YES completion:nil]; 149 150 } 151 } 152 153 - (void)imagePickerController: (UIImagePickerController *)picker 154 didFinishPickingMediaWithInfo: (NSDictionary *)info 155 { 156 self.titleLab.hidden = YES; 157 self.cameraImg.hidden = YES; 158 //获得原始的图片 159 UIImage* image = [info objectForKey: @"UIImagePickerControllerOriginalImage"]; 160 161 UIImage* newImage = [MSUtils scaleImage:image scaleFactor:0.2]; 162 NSData *fileData = UIImageJPEGRepresentation(newImage, 0.5); 163 164 self.photoImg.image = image; 165 166 if ([self.photoTypeStr isEqualToString:@"1000"]) 167 { 168 self.fileName = @"cidPhoto"; 169 } 170 else if ([self.photoTypeStr isEqualToString:@"1001"]) 171 { 172 self.fileName = @"driveCardPhoto"; 173 } 174 else if ([self.photoTypeStr isEqualToString:@"1002"]) 175 { 176 self.fileName = @"carPhoto"; 177 } 178 else if ([self.photoTypeStr isEqualToString:@"1003"]) 179 { 180 self.fileName = @"travelLicensePhoto"; 181 } 182 183 dict = [NSMutableDictionary dictionary]; 184 [dict setObject:self.photoTypeStr forKey:@"photoType"]; 185 [dict setObject:self.fileName forKey:@"imgName"]; 186 [dict setObject:image forKey:@"image"]; 187 [dict setObject:fileData forKey:@"fileData"]; 188 189 [self dismissViewControllerAnimated:YES completion:nil]; 190 } 191 192 - (IBAction)comfirBtnClick:(id)sender 193 { 194 if (self.photoImg.image == nil) { 195 UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"请选择照片"] delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil]; 196 [alertview show]; 197 198 return; 199 } 200 [self dismissViewControllerAnimated:YES completion:^{ 201 if ([self.myDelegate respondsToSelector:@selector(backImageName:)]) { 202 [self.myDelegate backImageName:dict]; 203 } 204 }]; 205 } 206 207 208 - (void)didReceiveMemoryWarning { 209 [super didReceiveMemoryWarning]; 210 // Dispose of any resources that can be recreated. 211 }
(2)主页面 ,主要看代理方法返回数据处理,以及网络请求部分,
1 #import "AddVehicleInfoViewController.h" 2 #import "DriverTableViewViewController.h" 3 #import "CarCellData.h" 4 5 #import "addPhotoImgViewController.h" 6 7 #define KALLNum @"0123456789" 8 #define IDCARDCHARACTERS @"0123456789X" 9 #define KALLNumAndDoc @"0123456789." 10 #define PASSWORDLIMIT @"1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm" 11 #define AddCar_yes @"YES"//添加 12 #define AddCar_no @"NO"//修改 13 14 @interface AddVehicleInfoViewController ()<MSAFClientDelegate,backImgNameDelegate,UIAlertViewDelegate> 15 { 16 NSData *fileData; 17 NSMutableArray *fileDataArray; 18 NSString *fileName; 19 NSMutableArray *fileNameArray; 20 NSString *photoPath;//每个图片的路径 21 } 22 //加边框 23 @property (strong, nonatomic) IBOutletCollection(UIView) NSArray *addBorderViews; 24 //姓名 25 @property (weak, nonatomic) IBOutlet UITextField *nameTF; 26 //身份证号 27 @property (weak, nonatomic) IBOutlet UITextField *cardTF; 28 //手机号 29 @property (weak, nonatomic) IBOutlet UITextField *mobileTF; 30 //车牌号 31 @property (weak, nonatomic) IBOutlet UITextField *numberTF; 32 //驾驶证号 33 @property (weak, nonatomic) IBOutlet UITextField *driveCardTF; 34 35 //车型大小是否选中图片 36 @property (weak, nonatomic) IBOutlet UIImageView *bigSizeIV; 37 @property (weak, nonatomic) IBOutlet UIButton *bigSizeBtn; 38 @property (weak, nonatomic) IBOutlet UIImageView *smallSizeIV; 39 @property (weak, nonatomic) IBOutlet UIButton *smallSizeBtn; 40 //是否是自卸车是否选中图片 41 @property (weak, nonatomic) IBOutlet UIImageView *dump_yesImg; 42 @property (weak, nonatomic) IBOutlet UIButton *dump_yesBtn; 43 @property (weak, nonatomic) IBOutlet UIImageView *dump_noImg; 44 @property (weak, nonatomic) IBOutlet UIButton *dump_noBtn; 45 //栏板类型图片 46 @property (weak, nonatomic) IBOutlet UIImageView *Height_ColumnBoardImg; 47 @property (weak, nonatomic) IBOutlet UIButton *HeightLBBtn; 48 @property (weak, nonatomic) IBOutlet UIImageView *Low_ColumnBoardImg; 49 @property (weak, nonatomic) IBOutlet UIButton *LowLBBtn; 50 @property (weak, nonatomic) IBOutlet UIImageView *theTabletImg; 51 @property (weak, nonatomic) IBOutlet UIButton *theTabletBtn; 52 53 54 //车长 55 @property (weak, nonatomic) IBOutlet UITextField *lengthTF; 56 //所有人 57 @property (weak, nonatomic) IBOutlet UITextField *ownerTF; 58 59 @property (weak, nonatomic) IBOutlet UIScrollView *myScrollView; 60 @property (weak, nonatomic) IBOutlet UIButton *confirmBtn; 61 62 63 @property (assign, nonatomic) double keyboardHeight; 64 @property (strong, nonatomic) BSUserInfo * userInfo; 65 @property (strong, nonatomic) MSAlertView * alertView; 66 67 @property (copy, nonatomic) NSString* infoStr;//车体类型 10大型 30小型 68 @property (copy, nonatomic) NSString* tipCarStr;//自卸车 1是 0否 69 @property (copy, nonatomic) NSString* tailgateStr;//栏板 PB-平板;DLB-低栏板;GLB-高栏板 70 // 71 @property (copy, nonatomic) NSString* carId;//0为新增,其他数字为修改。 72 //判断是修改还是添加 73 @property (copy, nonatomic) NSString* isAddCar;//0为新增,其他数字为修改。 74 //声明存储图片名的数组 75 @property (nonatomic, strong) NSMutableArray *addPhotoImgArray; 76 77 @end 78 79 @implementation AddVehicleInfoViewController 80 - (IBAction)tapContentView:(UITapGestureRecognizer *)sender { 81 [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; 82 } 83 84 //接收回传的值 85 -(void)passValue:(MyVehicleData *)value{ 86 self.carData=value; 87 // self.data.number=value.number; 88 // self.data.card=value.driverCid; 89 [self viewDidLoad]; 90 } 91 92 - (void)dealloc 93 { 94 self.alertView = nil; 95 self.userInfo = nil; 96 DLog(@"AddVehicleInfoViewController dealloc"); 97 } 98 99 - (void)viewDidLoad { 100 [super viewDidLoad]; 101 102 //目前只有新增功能 103 self.title = @"新增车辆"; 104 if (self.carData) 105 { 106 if (self.carData.info) { 107 self.infoStr = self.carData.info; 108 }else{ 109 self.infoStr = nil; 110 self.smallSizeIV.image = [UIImage imageNamed:@"rmpwd1_no"]; 111 self.bigSizeIV.image = [UIImage imageNamed:@"rmpwd1_no"]; 112 } 113 if (self.carData.tipCar) { 114 self.tipCarStr = self.carData.tipCar; 115 }else{ 116 self.tipCarStr = nil; 117 self.dump_yesImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 118 self.dump_noImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 119 } 120 if (self.carData.tailgate) { 121 self.tailgateStr = self.carData.tailgate; 122 }else{ 123 self.tailgateStr = nil; 124 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 125 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 126 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 127 } 128 } 129 else 130 { 131 self.infoStr = info_big; 132 self.tipCarStr = tipCar_no; 133 } 134 self.isAddCar = AddCar_yes; 135 136 self.userInfo = [BSUserInfo defaultUserInfo]; 137 self.alertView = [MSAlertView sharedAlertView]; 138 139 for (UIView*item in self.addBorderViews) { 140 item.layer.borderColor = [UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1].CGColor; 141 item.layer.borderWidth = 1; 142 } 143 self.nameTF.delegate = self; 144 self.cardTF.delegate = self; 145 self.mobileTF.delegate = self; 146 self.driveCardTF.delegate= self; 147 self.numberTF.delegate = self; 148 self.lengthTF.delegate = self; 149 self.ownerTF.delegate = self; 150 self.lengthTF.rightViewMode = UITextFieldViewModeAlways; 151 self.lengthTF.rightView = [self addUIView:@"米"]; 152 153 self.myScrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; 154 155 [[NSNotificationCenter defaultCenter] addObserver:self 156 selector:@selector(keyboardWillShown:) 157 name:UIKeyboardWillShowNotification object:nil]; 158 159 [[NSNotificationCenter defaultCenter] addObserver:self 160 selector:@selector(keyboardWillHide:) 161 name:UIKeyboardWillHideNotification object:nil]; 162 if (self.data != nil) { 163 self.isAddCar = AddCar_no; 164 self.title = @"车辆详情"; 165 self.confirmBtn.titleLabel.text = @"确认修改"; 166 167 //调换司机 168 UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"调换" style:UIBarButtonItemStylePlain target:self action:@selector(toDriver)]; 169 item.tintColor = [UIColor whiteColor]; 170 self.navigationItem.rightBarButtonItem = item; 171 172 [self.confirmBtn setTitle:@"确认修改" forState:UIControlStateNormal]; 173 [self modifyCarInfo]; 174 if ([self.data.shStatus isEqualToString:SHSTATUS_Through]) { 175 //通过审批的只能显示,不能编辑 176 [self setThrough]; 177 } 178 } 179 } 180 181 - (void)viewDidAppear:(BOOL)animated{ 182 [self viewDidLoad]; 183 184 } 185 - (void)toDriver 186 { 187 DriverTableViewViewController * vehiclePosition = [[DriverTableViewViewController alloc]initWithNibName:@"DriverTableViewViewController" bundle:nil]; 188 vehiclePosition.oldCarId=[self.data carId]; 189 vehiclePosition.driverId=[self.data driverId]; 190 vehiclePosition.delegate=self; 191 [self.navigationController pushViewController:vehiclePosition animated:YES]; 192 } 193 194 - (void)modifyCarInfo 195 { 196 //carId driveId 都不为0时代表修改,其他代表添加 197 self.carId = self.data.carId; 198 NSLog(@"???????? %@",self.data); 199 self.nameTF.text = self.data.name; 200 self.cardTF.text = self.data.card; 201 self.mobileTF.text = self.data.mobile; 202 self.driveCardTF.text= self.data.driverId; 203 204 205 if ([self.data.info isEqual:info_big]) { 206 self.infoStr = info_big; 207 self.bigSizeIV.image = [UIImage imageNamed:@"rmpwd1"]; 208 self.smallSizeIV.image = [UIImage imageNamed:@"rmpwd1_no"]; 209 } 210 else if ([self.data.info isEqual:info_small]) 211 { 212 self.infoStr = info_small; 213 self.smallSizeIV.image = [UIImage imageNamed:@"rmpwd1"]; 214 self.bigSizeIV.image = [UIImage imageNamed:@"rmpwd1_no"]; 215 } 216 else 217 { 218 self.infoStr = nil; 219 } 220 221 if (self.carData) 222 { 223 self.numberTF.text = self.carData.number; 224 self.lengthTF.text = self.carData.length; 225 self.ownerTF.text = self.carData.owner; 226 227 if ([self.carData.tipCar isEqual:tipCar_yes]) 228 { 229 self.tipCarStr = tipCar_yes; 230 self.dump_yesImg.image = [UIImage imageNamed:@"rmpwd1"]; 231 self.dump_noImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 232 } 233 else if ([self.carData.tipCar isEqual:tipCar_no]) 234 { 235 self.tipCarStr = tipCar_no; 236 self.dump_yesImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 237 self.dump_noImg.image = [UIImage imageNamed:@"rmpwd1"]; 238 } 239 else 240 { 241 self.tipCarStr = nil; 242 } 243 if ([self.carData.tailgate isEqual:tailgate_height]) 244 { 245 self.tailgateStr = tailgate_height; 246 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1"]; 247 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 248 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 249 } 250 else if ([self.carData.tailgate isEqual:tailgate_low]) 251 { 252 self.tailgateStr = tailgate_low; 253 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 254 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1"]; 255 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 256 } 257 else if ([self.carData.tailgate isEqual:tailgate_flat]) 258 { 259 self.tailgateStr = tailgate_flat; 260 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 261 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 262 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1"]; 263 } 264 else 265 { 266 self.tailgateStr = nil; 267 } 268 269 } 270 else 271 { 272 self.numberTF.text = self.data.number; 273 self.lengthTF.text = self.data.length; 274 self.ownerTF.text = self.data.owner; 275 276 if ([self.data.tipCar isEqual:tipCar_yes]) 277 { 278 self.tipCarStr = tipCar_yes; 279 self.dump_yesImg.image = [UIImage imageNamed:@"rmpwd1"]; 280 self.dump_noImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 281 } 282 else if ([self.data.tipCar isEqual:tipCar_no]) 283 { 284 self.tipCarStr = tipCar_no; 285 self.dump_yesImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 286 self.dump_noImg.image = [UIImage imageNamed:@"rmpwd1"]; 287 } 288 else 289 { 290 self.tipCarStr = nil; 291 } 292 293 if ([self.data.tailgate isEqual:tailgate_height]) 294 { 295 self.tailgateStr = tailgate_height; 296 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1"]; 297 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 298 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 299 } 300 else if ([self.data.tailgate isEqual:tailgate_low]) 301 { 302 self.tailgateStr = tailgate_low; 303 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 304 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1"]; 305 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 306 } 307 else if ([self.data.tailgate isEqual:tailgate_flat]) 308 { 309 self.tailgateStr = tailgate_flat; 310 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 311 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 312 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1"]; 313 } 314 else 315 { 316 self.tailgateStr = nil; 317 } 318 } 319 } 320 //通过审批,设置页面style 321 - (void)setThrough{ 322 self.confirmBtn.hidden = YES; //隐藏 323 324 //设置输入框的风格 325 self.nameTF.borderStyle = UITextBorderStyleNone; 326 self.cardTF.borderStyle = UITextBorderStyleNone; 327 self.mobileTF.borderStyle = UITextBorderStyleNone; 328 self.numberTF.borderStyle = UITextBorderStyleNone; 329 self.lengthTF.borderStyle = UITextBorderStyleNone; 330 self.ownerTF.borderStyle = UITextBorderStyleNone; 331 self.driveCardTF.borderStyle = UITextBorderStyleNone; 332 333 //设置输入框不可编辑 334 self.nameTF.enabled = NO; 335 self.cardTF.enabled = NO; 336 self.mobileTF.enabled = NO; 337 self.numberTF.enabled = NO; 338 self.lengthTF.enabled = NO; 339 self.ownerTF.enabled = NO; 340 self.driveCardTF.enabled= NO; 341 342 //状态按钮不可点击 343 self.bigSizeBtn.enabled = NO; 344 self.smallSizeBtn.enabled = NO; 345 self.dump_yesBtn.enabled = NO; 346 self.dump_noBtn.enabled = NO; 347 self.HeightLBBtn.enabled = NO; 348 self.LowLBBtn.enabled = NO; 349 self.theTabletBtn.enabled = NO; 350 } 351 //添加单位的view 352 - (UIView*)addUIView:(NSString*)unit 353 { 354 UIView* tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 15, 20)]; 355 UILabel* unitLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 15, 20)]; 356 unitLabel.text = unit; 357 unitLabel.textColor = [UIColor colorWithRed:189.0/255 green:189.0/255 blue:189.0/255 alpha:1]; 358 unitLabel.font = [UIFont systemFontOfSize:14]; 359 [tempView addSubview:unitLabel]; 360 return tempView; 361 } 362 - (IBAction)confirmClick:(UIButton *)sender { 363 if (self.nameTF.text.length == 0) 364 { 365 [self.alertView showAlertText:self WithText:@"请填写姓名" duration:1.5]; 366 [self.nameTF becomeFirstResponder]; 367 return; 368 } 369 if (self.cardTF.text.length == 0) 370 { 371 [self.alertView showAlertText:self WithText:@"请填写身份证号" duration:1.5]; 372 [self.cardTF becomeFirstResponder]; 373 return; 374 } 375 if (self.mobileTF.text.length != 11) 376 { 377 [self.alertView showAlertText:self WithText:@"手机号不为11位,请修改!" duration:1.5]; 378 [self.mobileTF becomeFirstResponder]; 379 return; 380 } 381 if (self.driveCardTF.text.length == 0) 382 { 383 [self.alertView showAlertText:self WithText:@"请填写驾驶证号" duration:1.5]; 384 [self.driveCardTF becomeFirstResponder]; 385 return; 386 } 387 if (self.numberTF.text.length == 0) 388 { 389 [self.alertView showAlertText:self WithText:@"请填写车牌号" duration:1.5]; 390 [self.numberTF becomeFirstResponder]; 391 return; 392 } 393 if (self.lengthTF.text.length == 0) 394 { 395 [self.alertView showAlertText:self WithText:@"请填写车长" duration:1.5]; 396 [self.lengthTF becomeFirstResponder]; 397 return; 398 } 399 if (self.ownerTF.text.length == 0) 400 { 401 [self.alertView showAlertText:self WithText:@"请填写车辆所有人" duration:1.5]; 402 [self.ownerTF becomeFirstResponder]; 403 return; 404 } 405 if (self.infoStr == nil) 406 { 407 [self.alertView showAlertText:self WithText:@"请选择车型" duration:1.5]; 408 return; 409 } 410 if (self.tipCarStr == nil) 411 { 412 [self.alertView showAlertText:self WithText:@"请选择自卸类型" duration:1.5]; 413 return; 414 } 415 if (self.tailgateStr == nil) 416 { 417 [self.alertView showAlertText:self WithText:@"请选择栏板类型" duration:1.5]; 418 return; 419 } 420 421 self.confirmBtn.enabled = NO; 422 //收键盘 423 [self tapContentView:nil]; 424 [self addCar]; 425 // [self.alertView showAlertText:self WithText:@"修改车辆信息中,请稍候" duration:1.5]; 426 [self.alertView startAnimating:self WithText:@"正在修改车辆信息,请稍候..."]; 427 [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(clickEnable) userInfo:nil repeats:NO]; 428 } 429 430 431 - (void)clickEnable 432 { 433 self.confirmBtn.enabled = YES; 434 } 435 436 - (void)viewDidDisappear:(BOOL)animated { 437 [super viewDidDisappear:animated]; 438 [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 439 } 440 - (void)didReceiveMemoryWarning { 441 [super didReceiveMemoryWarning]; 442 443 // Dispose of any resources that can be recreated. 444 } 445 #pragma mark ------ 按钮点击,切换选择的属性 ---------- 446 //车型切换 447 - (IBAction)changeTheCarSizeBtnClick:(UIButton *)button 448 { 449 //车体类型 10大型 30小型 450 [self changeImgStatesByTag:button.tag]; 451 } 452 //自装卸状态切换 453 - (IBAction)changeTheDumpStates:(UIButton *)button 454 { 455 //自卸车 1是tag=50 0否tag=70 456 [self changeImgStatesByTag:button.tag]; 457 } 458 //栏板类型切换 459 - (IBAction)changeTheColumnBoardStates:(UIButton *)button 460 { 461 //栏板 PB-平板tag=300;DLB-低栏板tag=200;GLB-高栏板tag=100 462 [self changeImgStatesByTag:button.tag]; 463 } 464 //通过btn的tag值来改变图片的状态 465 -(void)changeImgStatesByTag:(NSInteger)buttonTag 466 { 467 [self tapContentView:nil];//收键盘 468 if (buttonTag == 10)//大型 469 { 470 self.infoStr = info_big; 471 self.bigSizeIV.image = [UIImage imageNamed:@"rmpwd1"]; 472 self.smallSizeIV.image = [UIImage imageNamed:@"rmpwd1_no"]; 473 } 474 else if (buttonTag == 30)//小型 475 { 476 self.infoStr = info_small; 477 self.smallSizeIV.image = [UIImage imageNamed:@"rmpwd1"]; 478 self.bigSizeIV.image = [UIImage imageNamed:@"rmpwd1_no"]; 479 } 480 else if (buttonTag == 50)//是 自装卸 481 { 482 self.tipCarStr = tipCar_yes; 483 self.dump_yesImg.image = [UIImage imageNamed:@"rmpwd1"]; 484 self.dump_noImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 485 } 486 else if (buttonTag == 70)//否 487 { 488 self.tipCarStr = tipCar_no; 489 self.dump_noImg.image = [UIImage imageNamed:@"rmpwd1"]; 490 self.dump_yesImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 491 } 492 else if (buttonTag == 100)//高栏板 493 { 494 self.tailgateStr = tailgate_height; 495 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1"]; 496 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 497 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 498 } 499 else if (buttonTag == 200)//低栏板 500 { 501 self.tailgateStr = tailgate_low; 502 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 503 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1"]; 504 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 505 } 506 else if (buttonTag == 300)//平板 507 { 508 self.tailgateStr = tailgate_flat; 509 self.Height_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 510 self.Low_ColumnBoardImg.image = [UIImage imageNamed:@"rmpwd1_no"]; 511 self.theTabletImg.image = [UIImage imageNamed:@"rmpwd1"]; 512 } 513 } 514 #pragma mark --------- 跳转到图片页面的点击事件 ------- 515 - (IBAction)chooseOrSeeThePhotBtnClick:(UIButton *)sender 516 { 517 addPhotoImgViewController *APIVC = [[addPhotoImgViewController alloc] init]; 518 APIVC.myDelegate = self; 519 //tag 1000:身份证照片 1001:驾驶证照片 1002:车辆照片 1003:行驶证照片 520 APIVC.photoTypeStr = [NSString stringWithFormat:@"%ld",(long)sender.tag]; 521 APIVC.tapEnableStr = self.data.shStatus; 522 APIVC.photoArray = self.addPhotoImgArray; 523 if (sender.tag == 1000) 524 { 525 photoPath = self.data.cidPhoto; 526 } 527 else if (sender.tag == 1001) 528 { 529 photoPath = self.data.driveCardPhoto; 530 } 531 else if (sender.tag == 1002) 532 { 533 if (self.carData) 534 { 535 photoPath = self.carData.carPhoto; 536 537 } 538 else 539 { 540 photoPath = self.data.carPhoto; 541 } 542 } 543 else if (sender.tag == 1003) 544 { 545 if (self.carData) 546 { 547 photoPath = self.carData.travelLicensePhoto; 548 } 549 else 550 { 551 photoPath = self.data.travelLicensePhoto; 552 } 553 554 } 555 else 556 { 557 photoPath = nil; 558 } 559 APIVC.pathStr = self.path; 560 APIVC.imagePathStr = [NSString stringWithFormat:@"%@%@",self.path,photoPath]; 561 APIVC.photoPathStr = photoPath; 562 APIVC.shStatusStr = self.data.shStatus; 563 UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:APIVC]; 564 [self presentViewController:navc animated:YES completion:nil]; 565 } 566 -(void)backImageName:(NSDictionary *)dict 567 { 568 if (!self.addPhotoImgArray) 569 { 570 self.addPhotoImgArray = [NSMutableArray arrayWithCapacity:0]; 571 } 572 if (self.addPhotoImgArray.count>0) 573 { 574 NSDictionary *deleteDic = [NSDictionary dictionary]; 575 for (NSDictionary *diction in self.addPhotoImgArray) 576 { 577 //记录数组中需要删除的字典元素 578 if ([diction[@"photoType"] isEqualToString:dict[@"photoType"]]) 579 { 580 deleteDic = diction; 581 } 582 NSLog(@"****** %@",dict); 583 } 584 //记录数据之后统一进行删除,添加操作 585 [self.addPhotoImgArray removeObject:deleteDic]; 586 [self.addPhotoImgArray addObject:dict]; 587 NSLog(@"-------%ld",(long)self.addPhotoImgArray.count); 588 589 } 590 else 591 { 592 [self.addPhotoImgArray addObject:dict]; 593 NSLog(@"-------%ld",(long)self.addPhotoImgArray.count); 594 } 595 596 } 597 598 - (void)keyboardWillShown:(NSNotification*)aNotification{ 599 // DLog(@"%f",[self visibleKeyboardHeight]); 600 // 键盘信息字典 601 NSDictionary* info = [aNotification userInfo]; 602 NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; 603 CGRect keyboardRect = [aValue CGRectValue]; 604 int height = keyboardRect.size.height; 605 CGRect tempRect = CGRectMake(0, 0, ScreenRect.size.width, ScreenRect.size.height-NavigationbarHeight); 606 tempRect.size.height -= height; 607 self.myScrollView.frame = tempRect; 608 609 DLog(@"%@",info); 610 } 611 612 - (void)keyboardWillHide:(NSNotification*)aNotification{ 613 [UIView animateWithDuration:.5 animations:^{ 614 self.myScrollView.frame = CGRectMake(0, 0, ScreenRect.size.width, ScreenRect.size.height-NavigationbarHeight); 615 }]; 616 } 617 618 //限制输入内容 619 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 620 { 621 if ([string isEqual:@" "]) {//不允许输入空格 622 return NO; 623 } 624 switch (textField.tag) { 625 case 12: 626 { 627 if (textField.text.length >= 18 && string.length>0) { 628 [self.alertView showAlertText:self WithText:@"身份证号不超过18位"]; 629 return NO; 630 } 631 NSCharacterSet* cs = [[NSCharacterSet characterSetWithCharactersInString:IDCARDCHARACTERS]invertedSet]; 632 NSArray* arrayTemp = [string componentsSeparatedByCharactersInSet:cs]; 633 if ([string isEqual:@"x"]) { 634 self.cardTF.text = [self.cardTF.text stringByAppendingString:@"X"]; 635 } 636 return [string isEqualToString:[arrayTemp componentsJoinedByString:@""]]; 637 } 638 break; 639 case 13: 640 { 641 if (textField.text.length >= 11 && string.length>0) { 642 [self.alertView showAlertText:self WithText:@"手机号不超过11位"]; 643 return NO; 644 } 645 NSCharacterSet* cs = [[NSCharacterSet characterSetWithCharactersInString:KALLNum]invertedSet]; 646 NSArray* arrayTemp = [string componentsSeparatedByCharactersInSet:cs]; 647 return [string isEqualToString:[arrayTemp componentsJoinedByString:@""]]; 648 } 649 break; 650 case 14: 651 { 652 if (textField.text.length >= 20 && string.length>0) { 653 [self.alertView showAlertText:self WithText:@"密码不超过20位"]; 654 return NO; 655 } 656 NSCharacterSet* cs = [[NSCharacterSet characterSetWithCharactersInString:PASSWORDLIMIT]invertedSet]; 657 NSArray* arrayTemp = [string componentsSeparatedByCharactersInSet:cs]; 658 if ([string isEqualToString:[arrayTemp componentsJoinedByString:@""]]) { 659 return YES; 660 } 661 else 662 { 663 [self.alertView showAlertText:self WithText:@"密码只能由数字和字母组成!"]; 664 return NO; 665 } 666 667 } 668 break; 669 case 24: 670 { 671 NSCharacterSet* cs = [[NSCharacterSet characterSetWithCharactersInString:KALLNumAndDoc]invertedSet]; 672 NSArray* arrayTemp = [string componentsSeparatedByCharactersInSet:cs]; 673 674 //限制只让输入一个“.” 675 if ([string isEqual:@"."]&&[textField.text componentsSeparatedByString:@"."].count >1) { 676 return NO; 677 } 678 return [string isEqualToString:[arrayTemp componentsJoinedByString:@""]]; 679 } 680 break; 681 case 25: 682 { 683 NSCharacterSet* cs = [[NSCharacterSet characterSetWithCharactersInString:KALLNumAndDoc]invertedSet]; 684 NSArray* arrayTemp = [string componentsSeparatedByCharactersInSet:cs]; 685 //限制只让输入一个“.” 686 if ([string isEqual:@"."]&&[textField.text componentsSeparatedByString:@"."].count >1) { 687 return NO; 688 } 689 return [string isEqualToString:[arrayTemp componentsJoinedByString:@""]]; 690 } 691 break; 692 default: 693 break; 694 } 695 696 return YES; 697 } 698 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ 699 //返回一个BOOL值,指明是否允许在按下回车键时结束编辑 700 //如果允许要调用resignFirstResponder 方法,这回导致结束编辑,而键盘会被收起 701 [textField resignFirstResponder];//查一下resign这个单词的意思就明白这个方法了 702 return YES; 703 } 704 705 /***********************获取网络数据***********************/ 706 707 //添加车辆 708 709 - (void)addCar { 710 711 MSRequestPaket* requestPaket = [[MSRequestPaket alloc]initWithRequestId:request_addCar]; 712 713 //carId userId 都不为0时代表修改,其他代表添加 714 if ([self.isAddCar isEqual:AddCar_yes]) { 715 [requestPaket.parametersDic setObject:@"0" forKey:@"carId"]; 716 [requestPaket.parametersDic setObject:@"0" forKey:@"driverId"]; 717 } 718 else//修改 719 { //车辆Id 720 [requestPaket.parametersDic setObject:self.data.carId forKey:@"carId"]; 721 //司机Id 722 [requestPaket.parametersDic setObject:self.data.driverId forKey:@"driverId"]; 723 } 724 //用户Id 725 [requestPaket.parametersDic setObject:self.userInfo.userId forKey:@"userId"]; 726 //会员Id 727 [requestPaket.parametersDic setObject:self.userInfo.memberId forKey:@"memberId"]; 728 //姓名 729 [requestPaket.parametersDic setObject:self.nameTF.text forKey:@"name"]; 730 //身份证号 731 [requestPaket.parametersDic setObject:self.cardTF.text forKey:@"card"]; 732 //手机号 733 [requestPaket.parametersDic setObject:self.mobileTF.text forKey:@"mobile"]; 734 //车牌号 735 [requestPaket.parametersDic setObject:self.numberTF.text forKey:@"number"]; 736 //车型 737 [requestPaket.parametersDic setObject:self.infoStr forKey:@"info"]; 738 //车长 739 [requestPaket.parametersDic setObject:self.lengthTF.text forKey:@"length"]; 740 //所有人 741 [requestPaket.parametersDic setObject:self.ownerTF.text forKey:@"owner"]; 742 //驾驶证号 743 [requestPaket.parametersDic setObject:self.driveCardTF.text forKey:@"driveCard"]; 744 //自卸车 745 [requestPaket.parametersDic setObject:self.tipCarStr forKey:@"tipCar"]; 746 //栏板 747 [requestPaket.parametersDic setObject:self.tailgateStr forKey:@"tailgate"]; 748 749 fileNameArray = [NSMutableArray array]; 750 fileDataArray = [NSMutableArray array]; 751 for (int i=0; i<self.addPhotoImgArray.count; i++) 752 { 753 fileData = self.addPhotoImgArray[i][@"fileData"]; 754 [fileDataArray addObject:fileData]; 755 fileName = self.addPhotoImgArray[i][@"imgName"]; 756 [fileNameArray addObject:fileName]; 757 } 758 [[MSAFClient sharedClient] uploadMoreFileWithDataArray:fileDataArray andFileNameArray:fileNameArray andParameters:requestPaket.parametersDic Delegate:self]; 759 760 } 761 -(void)UploadCompeleted:(NSString *)result{ 762 763 [self.alertView stopAnimating]; 764 UIAlertView * alertview = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"操作成功"] delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles: nil]; 765 alertview.tag = 1001; 766 [alertview show]; 767 return; 768 DLog(@"上传成功--%@",result); 769 } 770 771 -(void)UploadFail:(NSString *)errorString{ 772 DLog(@"操作失败"); 773 [self.alertView stopAnimating]; 774 [MSUtils showAlertViewWithError:errorString]; 775 } 776 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 777 { 778 if (alertView.tag == 1001) 779 { 780 [self returnBack]; 781 } 782 } 783 @end
网络请求的封装方法:(注意内部的数组处理)
1 //上传文件(data) 2 -(void)uploadMoreFileWithDataArray:(NSArray*)dataArray andFileNameArray:(NSArray*)fileNameArray andParameters:(NSDictionary*)parameters Delegate:(id<MSAFClientDelegate>)delegate{ 3 4 NSURL* url = [[NSURL alloc]initWithString:MSDefaultBaseURL]; 5 AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:url]; 6 manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 7 NSMutableURLRequest* mutableRequest = [[AFHTTPRequestSerializer serializer]requestWithMethod:@"GET" URLString:uploadPath parameters:parameters error:nil]; 8 [manager POST:mutableRequest.URL.relativeString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 9 //解析出文件名 10 for (int i=0; i<dataArray.count; i++) { 11 12 // self.array_fileName = dataArray[i]; 13 self.array_fileData = dataArray[i]; 14 self.name = [fileNameArray[i] stringByDeletingPathExtension]; 15 [formData appendPartWithFileData:self.array_fileData 16 name:self.name 17 fileName:self.name 18 mimeType:@"multipart/form-data"]; 19 } 20 } success:^(AFHTTPRequestOperation *operation, id responseObject) { 21 //成功回调 22 DLog(@"upload success=====%@",operation.responseString); 23 [delegate UploadCompeleted:nil]; 24 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 25 26 //失败回调 27 DLog(@"upload failed=====%@",[error localizedDescription]); 28 DLog(@"------%@",[error.userInfo objectForKey:@"NSLocalizedRecoverySuggestion"]); 29 [delegate UploadFail:[error localizedDescription]]; 30 }]; 31 32 }