您的位置:首页 > 其它

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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: