AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// [manager.requestSerializer setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
NSString *urlString = UpLoadUrl;
NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
//post请求
[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"YYYY-MM-dd-hh:mm:ss:SSS";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];
/*
此方法参数
1. 要上传的[二进制数据]
2. 我这里的imgFile是对应后台给你url里面的图片参数,别瞎带。
3. 要保存在服务器上的[文件名]
4. 上传文件的[mimeType]
*/
[formData appendPartWithFileData:imageData name:@"mfile" fileName:fileName mimeType:@"image/jpeg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
float progress = 1.0 * uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
// NSLog(@"上传图片进度%f",progress);
//找到message大数组中对应数据以更改,加入上传进度
NSString *who;
int which = 0;
NSString *file1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"message.count"];
if (![[NSFileManager defaultManager] fileExistsAtPath:file1]) {
}else{
NSMutableDictionary *countDic = [[NSMutableDictionary alloc]initWithContentsOfFile:file1];
// NSLog(@"%@",countDic);
NSString* time = [NSString stringWithFormat:@"%ld",timeStamp];
NSMutableDictionary *valueDic = [countDic objectForKey: time];
who = [valueDic objectForKey:@"to"];
which = [[valueDic objectForKey:@"count"] intValue];
NSData *userData = [[NSUserDefaults standardUserDefaults] objectForKey:K_USER_USERMESSAGE];
MxmUserMessage *mxmUserModel = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
NSString *my = mxmUserModel.userName;
NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"message.data"];
if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
}else{
NSMutableDictionary *aDic = [[NSMutableDictionary alloc]initWithContentsOfFile:file];
NSMutableArray *dataArr = [aDic objectForKey:my];
for (int i = 0; i<dataArr.count; i++) {
NSMutableArray *smallArr = [dataArr objectAtIndex:i];
//下面三句只是为了拿到samllArr中每个字的key(每个字典的key都是相同的)
NSMutableDictionary *msgDic = [smallArr objectAtIndex:0];
NSArray *keyArr = msgDic.allKeys;
NSString *key = keyArr[0];
if ([key isEqualToString:who]) {
NSMutableDictionary *msgDic1 = [smallArr objectAtIndex:which -1];
NSMutableDictionary *keyDic = [msgDic1 objectForKey:key];
[keyDic setObject:@"1" forKey:@"isSender"];
[keyDic setObject:[NSString stringWithFormat:@"%f",progress] forKey:@"uploadProgress"];
[msgDic1 setObject:keyDic forKey:key];
[smallArr replaceObjectAtIndex:which -1 withObject:msgDic1];
[dataArr replaceObjectAtIndex:i withObject:smallArr];
[aDic setObject:dataArr forKey:my];
[aDic writeToFile:file atomically:YES];
// NSLog(@"加入了上传进度-- 第%d句,时间是%@",which,time);
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]postNotificationName:@"loadDataSource" object:nil];
});
}
}
}
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSString *errmsg = [responseObject objectForKey:@"errmsg"];
NSString *mediaID = [responseObject objectForKey:@"mediaid"];
if (mediaID && [errmsg isEqualToString:@"ok"]) {
NSLog(@"上传成功");
}
NSString* urlStr= [NSString stringWithFormat:@"%@/%@",DownLoadUrl,mediaID];
NSLog(@"发送的%@===%@",urlStr,path);
if (your.length <36) {
[vc sendMessageWithContent:urlStr localUrl:path type:type realTime:timeStamp];
}else{
JFGroupChatController *groupVC = [[JFGroupChatController alloc]init];
[groupVC sendMessageWithContent:urlStr localUrl:path type:type realTime:timeStamp];
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败:%@",error);
}];
AFN上传图片
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 利用AFN封装上传多张图片方法,并压缩图片体积 对您有用的话,可否关注下本人?以此激励我和您的共同提高,谢谢了. ...