iOS上传语音、文字、图片(分类 多张)

**更新原因:后来项目中上传的图片涉及到了 多类,每一类中的图片都有多张。在2016年7月4日更新这篇博客。**

需求:用户可能提交 文字,也可能有语音,还有可能有图片。当然 都有也是可以的。
在这里我的方案是 先做判断(因为我提交的类不同(也就是方法不一样));
1if (_recorderData == nil && _imageDataInfoArr.count == 0 && _imageDataDrugArr.count == 0 && _imageDataCheckArr.count ==0){
  //只有文字
}else{

    // 有多组图片  多条文字参数  单个语音文件(多个的道理跟多组图片的逻辑一样)
}

方法:

// 有多组图片  多条文字参数  单个语音文件(多个的道理跟多组图片的逻辑一样)
+(void)askTextImageByUserInfoId:(NSString *)userInfoId
             WithUserRelationId:(NSString *)userRelationId
               WithDoctorInfoId:(NSString *)doctorInfoId
                      WithMoney:(NSString *)money
                    WithPayType:(NSString *)payType
                 WithAskContent:(NSString *)askContent
             WithClinicHospital:(NSString *)clinicHospital
              WithDrugTreatment:(NSString *)drugTreatment
             WithOtherTreatment:(NSString *)otherTreatment
         WithImageDataInfoArray:(NSMutableArray *)imageDataInfoArr
         WithImageDataDrugArray:(NSMutableArray *)imageDataDrugArr
        WithImageDataCheckArray:(NSMutableArray *)imageDataCheckAr
                  WithVoiceData:(NSData *)voiceData
                        success:(void (^)(id askDescArray))success
                        failure:(void (^)(NSError *error))failure{

具体实现:


 NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];

    //接口方法名
    req.methodName = @"insertOnLineAskInfoPicNew";

    //提交的 参数 信息
    [req setValue:userInfoId forKey:@"user_info_id"];
    [req setValue:userRelationId forKey:@"user_relation_id"];
    [req setValue:askContent forKey:@"ask_content"];
    [req setValue:doctorInfoId forKey:@"doctor_info_id"];
    [req setValue:money forKey:@"money"];
    [req setValue:payType forKey:@"pay_type"];

    [req setValue:clinicHospital forKey:@"clinic_hospital"];
    [req setValue:drugTreatment forKey:@"drug_treatment"];
    [req setValue:otherTreatment forKey:@"other_treatment"];


这里的  req  就是下面方法中的第一个参数 NetWorkBaseRequest 累(自己做的一个字典。可以家请求的网址  里面的有个字典 也就是各个参数)

/*______在线 或者是 电话   上传 所问问题   包含文字 图片(声音)________**littleSun_zheng**/
/**
 *  请求Opinion Server Api 接口方法
 *
 *  @param request 自定义的Request请求
 *  @param userInfoId 对应人的唯一标示(用户本人  和  关系人)
 *  @param token token值

 *  @param imageDataInfoArr  第一类图片  里面有一张 或者多张
 *  @param imageDataDrugArr  第二类图片  里面有一张 或者多张
 *  @param imageDataCheckArr  第三类图片  里面有一张 或者多张  因为写的是博客  我没有写用途,对参考者来讲也没什么用

 *  @param success
 *  @param failure 
 *  @param cached
 */
+ (void)requestWithPOST:(NetWorkBaseRequest *)request
 WithImageDataInfoArray:(NSMutableArray *)imageDataInfoArr
 WithImageDataDrugArray:(NSMutableArray *)imageDataDrugArr
WithImageDataCheckArray:(NSMutableArray *)imageDataCheckArr
          WithVoiceData:(NSData *)voiceData
                success:(void (^)(NetWorkBaseResponse *responseObject))success
                failure:(void (^)(NSError *error))failure
                  cache:(BOOL)cached
{



    if (!request) {
        NSLog(@"[ERROR] request cannot be NULL");
        if (failure) {

            failure([NSError errorWithDomain:@"" code:-1 userInfo:nil]);
        }
        return;
    }

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    [manager.requestSerializer setValue:@"multipart/form-data;charset=utf-8"forHTTPHeaderField:@"Content-Type"];

    manager.requestSerializer.timeoutInterval = 20;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];

    NSString *tokenValue = [SSKeychain passwordForService:[AppUtils appServiceName] account:[AppUtils tokenKey]];
    处理tokenValue的问题,我的逻辑。

    [manager POST:request.stringOfApiURLString parameters:request.paramDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //对传进来的 图片  数组进行遍历
        //病情描述图片
        for (int i = 0; i < imageDataInfoArr.count; i++) {

            NSData *imgData = imageDataInfoArr[i];

            //用这个当图片的名字,一起到图片名不重复的效果
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            NSString *tupianming = [NSString stringWithFormat:@"yyyyMMddHHmmssSSSS%d",i];
            [dateFormatter setDateFormat:tupianming];
            NSString *pictname = [NSString stringWithFormat:@"/%@.jpg",[dateFormatter stringFromDate:[NSDate date]]];
            NSString *picFileName = [NSString stringWithFormat:@"imageInfoKey"];

            [formData appendPartWithFileData:imgData name:picFileName fileName:[NSString stringWithFormat:@"%@",pictname] mimeType:@"image/jpg"];
        }
        //药品图片
        for (int i = 0; i < imageDataDrugArr.count; i++) {

            NSData *imgData = imageDataDrugArr[i];

            //用这个当图片的名字,一起到图片名不重复的效果
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            NSString *tupianming = [NSString stringWithFormat:@"yyyyMMddHHmmssSSSS%d",i];
            [dateFormatter setDateFormat:tupianming];
            NSString *pictname = [NSString stringWithFormat:@"/%@.jpg",[dateFormatter stringFromDate:[NSDate date]]];
            NSString *picFileName = [NSString stringWithFormat:@"imageDrugKey"];

            [formData appendPartWithFileData:imgData name:picFileName fileName:pictname mimeType:@"image/jpg"];
        }
        //检查  图片
        for (int i = 0; i < imageDataCheckArr.count; i++) {

            NSData *imgData = imageDataCheckArr[i];

            //用这个当图片的名字,一起到图片名不重复的效果
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            NSString *tupianming = [NSString stringWithFormat:@"yyyyMMddHHmmssSSSS%d",i];
            [dateFormatter setDateFormat:tupianming];
            NSString *pictname = [NSString stringWithFormat:@"/%@.jpg",[dateFormatter stringFromDate:[NSDate date]]];
            NSString *picFileName = [NSString stringWithFormat:@"imageCheckKey"];

            [formData appendPartWithFileData:imgData name:picFileName fileName:pictname mimeType:@"image/jpg"];
        }

        //语音文件数据
        if(voiceData!=nil){

            //用这个当图片的名字,起到图片名不重复的效果
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            NSString *tupianming = [NSString stringWithFormat:@"yyyyMMddHHmmssSSSS%@",@"luyin"];
            [dateFormatter setDateFormat:tupianming];
            NSString *luyinFileName = [NSString stringWithFormat:@"voiceKey"];
            //让服务器识别这个音频的格式 为  amr
            NSString * luyinname = [NSString stringWithFormat:@"/%@.amr",[dateFormatter stringFromDate:[NSDate date]]];

            [formData appendPartWithFileData:voiceData name:luyinFileName fileName:luyinname mimeType:@"audio/amr"];

        }

    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSDictionary *dic = responseObject;//operation.request.cachePolicy

        if (dic){

            @try{
                //解决若是json格式出错崩溃问题,判断status=0为成功
                NSString *responseClassName =  [NetWorkUtil replaceCharacter:@"Request" withString:@"Response" inString:NSStringFromClass([request class])];
                NetWorkBaseResponse *response = [[NSClassFromString(responseClassName) alloc] initWithDictionary:dic];
                if (response && success) {

                    success(response);

                }else{
                    failure([NSError errorWithDomain:@"接口出错" code:-1 userInfo:nil]);
                }
            }@catch(NSException *exception) {
                failure([NSError errorWithDomain:@"接口返回数据格式出错" code:-1 userInfo:nil]);
            }
        }
        else if (failure){
            failure([NSError errorWithDomain:@"接口出错" code:-1 userInfo:nil]);
        }else{
            NSLog(@"[ERROR] failure not exist!");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];



}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值