AFNetWorking上传图片
2016-04-21 16:12
381 查看
[manager POST:[rootPathstringByAppendingString:@"pos/order/upload"]parameters:@{@"oid":_ticketModel.tickectId}constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
NSInteger imgCount =0;
NSMutableArray *imageDatas = [NSMutableArrayarray];
for (int i =0; i<photos.count; i++) {
NSData * imageData =UIImageJPEGRepresentation(photos[i],0.5);
[imageDatas addObject:imageData];
}
for (NSData *imageDatain imageDatas) {
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
formatter.dateFormat =@"yyyyMMddHHmmss";
NSString *fileName = [NSStringstringWithFormat:@"IMG_%@%@.png",[formatterstringFromDate:[NSDatedate]],@(imgCount)];
[formData appendPartWithFileData:imageDataname:fileName fileName:fileNamemimeType:@"image/png"];
imgCount++;
}
} progress:^(NSProgress *_Nonnull uploadProgress) {
NSLog(@"上传数据量:%lld",uploadProgress.completedUnitCount);
<span style="white-space:pre"> </span>float totalCount = uploadProgress.totalUnitCount;
float completeCount = uploadProgress.completedUnitCount;
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3 animations:^{
progressView.alpha = 1.0f;
}completion:^(BOOL finished) {
progressView.hidden = NO;
[progressView setProgress:(completeCount/totalCount) animated:YES];
progressView.progressLabel.font = [UIFont systemFontOfSize:10];
progressView.progressLabel.textColor = [UIColor purpleColor];
progressView.progressLabel.text = [NSString stringWithFormat:@"%.2lf%%",(completeCount/totalCount)*100];
}];
});
} success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
NSString *string = [[NSStringalloc] initWithData:responseObjectencoding:NSUTF8StringEncoding];
if ([stringisEqualToString:@"success"]) {
[HUDshowInView:self.viewtext:@"图片上传成功"];
}else{
[HUDshowInView:self.viewtext:@"图片上传失败"];
}
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
}];
fromData 用来拼接要上传的图片数据,要压缩处理下图片,不然上传失败。目前是这样的。
注意上传进度要回到主线程刷新UI。
NSInteger imgCount =0;
NSMutableArray *imageDatas = [NSMutableArrayarray];
for (int i =0; i<photos.count; i++) {
NSData * imageData =UIImageJPEGRepresentation(photos[i],0.5);
[imageDatas addObject:imageData];
}
for (NSData *imageDatain imageDatas) {
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
formatter.dateFormat =@"yyyyMMddHHmmss";
NSString *fileName = [NSStringstringWithFormat:@"IMG_%@%@.png",[formatterstringFromDate:[NSDatedate]],@(imgCount)];
[formData appendPartWithFileData:imageDataname:fileName fileName:fileNamemimeType:@"image/png"];
imgCount++;
}
} progress:^(NSProgress *_Nonnull uploadProgress) {
NSLog(@"上传数据量:%lld",uploadProgress.completedUnitCount);
<span style="white-space:pre"> </span>float totalCount = uploadProgress.totalUnitCount;
float completeCount = uploadProgress.completedUnitCount;
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3 animations:^{
progressView.alpha = 1.0f;
}completion:^(BOOL finished) {
progressView.hidden = NO;
[progressView setProgress:(completeCount/totalCount) animated:YES];
progressView.progressLabel.font = [UIFont systemFontOfSize:10];
progressView.progressLabel.textColor = [UIColor purpleColor];
progressView.progressLabel.text = [NSString stringWithFormat:@"%.2lf%%",(completeCount/totalCount)*100];
}];
});
} success:^(NSURLSessionDataTask *_Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
NSString *string = [[NSStringalloc] initWithData:responseObjectencoding:NSUTF8StringEncoding];
if ([stringisEqualToString:@"success"]) {
[HUDshowInView:self.viewtext:@"图片上传成功"];
}else{
[HUDshowInView:self.viewtext:@"图片上传失败"];
}
} failure:^(NSURLSessionDataTask *_Nullable task, NSError *_Nonnull error) {
}];
fromData 用来拼接要上传的图片数据,要压缩处理下图片,不然上传失败。目前是这样的。
注意上传进度要回到主线程刷新UI。
相关文章推荐
- SDAU 课程练习3 1001
- Unity之AssetBundles读取打包对象
- 百度地图学习(一):加载基本地图、本地定位、地理编码
- MongoDB服务无法启动-10061由于目标计算机积极拒绝,无法连接
- javascript-点击栏目条增加操作框
- Android Fragment详解
- Android Studio 打包混淆出现的问题
- hdu 3785
- Oracle常见故障解析(3):win7运行sqlplus报错“SP2-1503: 无法初始化 Oracle 调用界面”
- Centos中mount命令挂载windows7共享目录
- lamda表达式 随机取数据的方法
- 如何用 Robotframework 来编写优秀的测试用例
- [leetcode] 68. Text Justification
- 2016.4 半期 射箭馆,爱打怪兽的何老板-----KMP
- lightoj 1048 - Conquering Keokradong 【二分 + 贪心】
- Spring学习——Aop原理
- 二级指针操作指针数组
- 杭电1180
- key-value数据库优点
- 卡片布局