-(void)uploadImage:(NSData*)imgData{
// [AppUtils showProgressMessage:@"正在上传图片..." inView:self.view];
NSURLSessionConfiguration *configSession
=[NSURLSessionConfiguration defaultSessionConfiguration];
//设置缓存策略
configSession.requestCachePolicy=NSURLRequestReloadIgnoringCacheData;
configSession.timeoutIntervalForRequest=15; //设置超时时间
AFHTTPSessionManager *manager=[[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:ApiURL] sessionConfiguration:configSession];
[manager.responseSerializer setAcceptableContentTypes:
[NSSet setWithObjects:
@"text/plain",
@"application/json",
@"text/json",
@"text/javascript",
@"text/html",
nil]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[managerPOST:url_uploadimage parameters:@{@"is_censor":@"2"} headers:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
if(imgData!=nil) {
//采用时间来防止名字重复
NSDateFormatter*formatter = [[NSDateFormatteralloc]init];
formatter.dateFormat=@"yyyyMMddHHmmss";
NSString*str = [formatterstringFromDate:[NSDatedate]];
NSString*fileName = [NSStringstringWithFormat:@"%@.jpg", str];
[formDataappendPartWithFileData:imgDataname:@"image"fileName:fileNamemimeType:@"image/jpg"];
}
}progress:^(NSProgress*_NonnulluploadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
if([kStringFormat(@"%@",responseObject[@"code"])isEqualToString:@"200"]) {
_dataModel.head_100=responseObject[@"data"][@"image"];
[self changePhotoRequest];
}else{
[AppUtilsdismissHUDInView:self.view];
[AppUtilsshowSuccessMessage:kStringFormat(@"%@",responseObject[@"msg"])inView:self.view];
}
NSLog(@"--------%@",responseObject);
}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {
[AppUtils dismissHUDInView:self.view];
NSLog(@"========%@",error);
}];
}
网友评论