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<UIImage *> * _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:@"请求失败"];
    }];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 概要 64学时 3.5学分 章节安排 电子商务网站概况 HTML5+CSS3 JavaScript Node 电子...
    阿啊阿吖丁阅读 9,350评论 0 3
  • 自1月15起来工作起,已经一个半月了,细想起来,总体上感觉自己稀稀疏疏,没有个样子,这不应该是我的作风,更重要的是...
    意守两眉阅读 274评论 0 0
  • A:同事前期一直被合作伙伴欺负、忽悠,经过这几天自己摸清了他们这几年的来龙去脉,找到了关键点 M:气愤 B:在合适...
    李鼎_6611阅读 158评论 0 0
  • 大付老师(付业兴),语文老师辞职,立志成为自由职业插画师,一晃做了十多年,也写过一些小诗,也是儿童美术培训机构金蓓...
    插画师付业兴阅读 143评论 0 4
  • 下夜班,晚上十点,开车回家。 白天只补觉两个小时左右,没睡够,有些恍惚。行车到红灯时,踩刹车,却发...
    明妙阅读 112评论 0 0