直接干货
+ (void)postWithImage:(UIImage *)Image Url:(NSString *)url params:(NSDictionary *)params successBlock:(void(^)(id responseObject))completion Failure:(Failure)failureBlock{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
[set addObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = set;
manager.requestSerializer.timeoutInterval = 8;
NSString *urlString = [NSString stringWithFormat:@"%@%@",BASE_URL,url];//服务器的url
[manager POST:urlString parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(Image, 0.1) name:@"pic" fileName:@"image.jpeg" mimeType:@"image/jpeg"]; //这儿如果是需要保证图片的质量,那么就用UIImagePNGRepresentation(Image) 但是这样的时间可能要慢一些 对图片的要求不高就用UIImageJPEGRepresentation(Image, 0.1) 0.1 表示压缩的程度
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
//成功block
if (completion) {
completion(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失败block
if (failureBlock) {
failureBlock(error);
}
}];
}
网友评论