iOS-快速实现上传认证资料(PhotoBrowser)

实现流程
1.布局(不列代码,展示图)

iOS-快速实现上传认证资料(PhotoBrowser)_第1张图片
12.png

2.取得本地选择图片path

-(void)actionSheetStart:(NSString *)type{
    
    actionSheet = [[ZLPhotoActionSheet alloc] init];
    //设置照片最大选择数
    actionSheet.maxSelectCount = 1;
    //设置照片最大预览数
    actionSheet.maxPreviewCount = 20;
    [actionSheet showWithSender:self animate:YES completion:^(NSArray * _Nonnull selectPhotos) {
        if (selectPhotos.count == 0) {
            return ;
        }
        [photosArray addObjectsFromArray:selectPhotos];
        
        
        for (UIImage *image  in selectPhotos) {
            [_selectImageArray removeAllObjects];
            [_selectImageArray addObjectsFromArray:selectPhotos];
            NSData *data;
            //返回为JPEG图像。
            data = UIImageJPEGRepresentation(image, 0.7);
            //保存
            //获取Documents文件夹目录
            NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentPath = [path objectAtIndex:0];
            //指定新建文件夹路径
            NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageIcoFile"];
            //创建ImageFile文件夹
            [[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"YYYYMMddhhmmssSSS"];
            NSString *currentDay = [dateFormatter stringFromDate:[NSDate date]];//当前年月日
            
            //保存图片的路径
            NSString *imgPath = [imageDocPath stringByAppendingPathComponent:currentDay];
            imgPath = [NSString stringWithFormat:@"%@.png",imgPath];
            [[NSFileManager defaultManager] createFileAtPath:imgPath contents:data attributes:nil];
//调用上传图片方法
           [self subImageData:imgPath andImage:image type:@"3"];
            
             _imagepath = imgPath;
            if(_imagepath != nil){
                if ([self.updelegate respondsToSelector:@selector(PopImageString:andImage:withArr:type:)]) {
                    [self.updelegate PopImageString:_imagepath andImage:image withArr:updatedImageUrlArray type:type];
                }
                [self back];
            }
  
        }
   
    }];

}

3.将图片Path传给服务器取得服务器返回的"图片名称"(在选择确定了就上传给服务器了)

-(void)subImageData:(NSString *)imagePath andImage:(UIImage *)image type:(NSString *)type
{
    [MBProgressHUD showMessag:@"上传图片" toView:nil];
    NSString *userID = [UserManager sharedUserManager].user.userid;
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithCapacity:4];
    [parameter setValue:userID forKey:@"userid"];
    [parameter setValue:type forKey:@"type"];
    
    [[RequestManager shareManager] requestDataWithRequestType:GetUploadShowImage parameters:parameter filePath:imagePath httpMethod:KHTTPPOST completionHandler:^(ResponseItem *respones) {
        
        RequestStatus status = [respones.dataDic[KResult] integerValue];
        if (status == RequestStatusSuccess)
        {
            [MBProgressHUD hideHUDForView:nil animated:YES];
            NSString *imageURL = respones.dataDic[@"photo"];
            NSMutableDictionary *imageDic = [NSMutableDictionary dictionaryWithCapacity:4];
            [imageDic setObject:imageURL forKey:@"image"];
            [updatedImageUrlArray addObject:imageDic];
        }else
        {
            [self showHUDErrorWithStatus:@"图片上传失败"];
            [MBProgressHUD hideHUDForView:nil animated:YES];
            [photosArray removeObject:image];
        }
    } failedHandler:^(NSError *error) {
        [self showHUDErrorWithStatus:@"请求失败"];
        [MBProgressHUD hideHUDForView:nil animated:YES];
        [photosArray removeObject:image];
    }];
}

3.转JSON把服务器返回的"图片名称"提交给服务器

-(void)SubmitImageUrl:(NSMutableArray *)imageURLArray andType:(NSString *)type
{
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:imageURLArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *imageUrlJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *userID = [UserManager sharedUserManager].user.userid;
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithCapacity:4];
    [parameter setValue:userID forKey:@"userid"];
    [parameter setValue:type forKey:@"authenticateType"];
    [parameter setValue:imageUrlJson forKey:@"image"];
    
    
    [MBProgressHUD showMessag:@"正在提交" toView:nil];
    [[RequestManager shareManager]requestDataWithRequestType:AuditEntUser parameters:parameter filePath:@"" httpMethod:KHTTPPOST completionHandler:^(ResponseItem *respones) {
        NSLog(@"%@",respones.dataDic);
        [MBProgressHUD hideHUDForView:nil animated:YES];
        RequestStatus status = [respones.dataDic[KResult] integerValue];
        if (status == RequestStatusSuccess)
        {

            [self.view addSubview:self.pView];

        }else
        {
           
            
            
            [self showHUDErrorWithStatus:@"提交失败"];
        }
        
    } failedHandler:^(NSError *error) {
       
    
        [self showHUDErrorWithStatus:@"请求失败"];
    }];
}

你可能感兴趣的:(iOS-快速实现上传认证资料(PhotoBrowser))