标签:
需求:用户可能提交 文字,也可能有语音,还有可能有图片。当然 都有也是可以的。
在这里我的方案是 先做判断(因为我提交的类不同(也就是方法不一样));
(1)if (_imageArray.count == 0 && recordingInterval == 0){
//只有文字
}else{
// 文字 图片 加录音
}
方法:
// 有文字,有图,片有语音
+(void)askTextImageByUserInfoId:(NSString *)userInfoId
WithUserRelationId:(NSString *)userRelationId
WithAskContent:(NSString *)askContent
WithImageDataArray:(NSMutableArray *)imageDataArray
WithVoiceData:(NSData *)voiceData
success:(void (^)(id askDescArray))success
failure:(void (^)(NSError *error))failure`
具体实现:
/**
* 请求Opinion Server Api 接口方法
*
* @param request 自定义的Request请求
* @param userInfoId 对应人的唯一标示(用户本人 和 关系人)
* @param token token值
* @param imageAndVoiceDataArray 盛放图片 语音的数组
* @param success 请求成功,返回Response对象
* @param failure 请求失败
* @param cached 是否进行缓存
*/
+ (void)requestWithPOST:(NetWorkBaseRequest *)request WithImageDataArray:(NSMutableArray *)imageDataArray 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 = 10;
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
NSString *tokenValue = [SSKeychain passwordForService:[AppUtils appServiceName] account:[AppUtils tokenKey]];
if (!tokenValue) {
tokenValue = @"";
}else{
[request.paramDic setValue:tokenValue forKey:@"token"];
}
[manager POST:request.stringOfApiURLString parameters:request.paramDic constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
//对传进来的 图片 数组进行遍历
for (int i = 0; i < imageDataArray.count; i++) {
NSData *imgData = imageDataArray[i];
//用这个当图片的名字,一起到图片名不重复的效果
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *tupianming = [NSString stringWithFormat:@"yyyyMMddHHmmssSSSS%d",i];
[dateFormatter setDateFormat:tupianming];
NSString *pictname;
NSString *picFileName;
pictname = [NSString stringWithFormat:@"/%@.jpg",[dateFormatter stringFromDate:[NSDate date]]];
picFileName = [NSString stringWithFormat:@"/%@file",[dateFormatter stringFromDate:[NSDate date]]];
[formData appendPartWithFileData:imgData name:picFileName fileName:[NSString stringWithFormat:@"%@",pictname] mimeType:@"image/jpg"];
}
if(voiceData!=nil){
//用这个当图片的名字,起到图片名不重复的效果
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *tupianming = [NSString stringWithFormat:@"yyyyMMddHHmmssSSSS%@",@"luyin"];
[dateFormatter setDateFormat:tupianming];
NSString *luyinname;
NSString *luyinFileName;
luyinname = [NSString stringWithFormat:@"/%@.amr",[dateFormatter stringFromDate:[NSDate date]]];
//让服务器识别这个音频的格式 为 amr
luyinFileName = [NSString stringWithFormat:@"/%@.amr",[dateFormatter stringFromDate:[NSDate date]]];
[formData appendPartWithFileData:voiceData name:luyinname fileName:luyinFileName 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);
}];
}
标签:
原文地址:http://blog.csdn.net/littlesun_zheng/article/details/51331350