以前往服务器上传图片的时候傻傻的自己写上传,不晓得afnetworking上传图片方法,今天整理出来以做记录
参见http://blog.csdn.net/zwl492454828/article/details/52160555
AFHTTPSessionManager *manager = [AFHTTPSessionManagermanager];
manager.responseSerializer.acceptableContentTypes
[NSSetsetWithObjects:@"application/json",@"text/html",@"image/jpeg",@"image/png",@"application/octet-stream",@"text/json",nil];
NSString *url=@"";
NSURLSessionDataTask *task = [managerPOST:url parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData>_Nonnull formData)
{
UIImage * image = [UIImageimageNamed:@"test.png"];
NSData *imageData =UIImageJPEGRepresentation(image,1);
/*将图片进行压缩
NSData *imageData
if (UIImagePNGRepresentation(image) == nil) {
self.imageData = UIImageJPEGRepresentation(image, 1);
} else {
self.imageData = UIImagePNGRepresentation(image);
}
*/
NSString *str =@"根据需求自己命名";
NSString *fileName = [NSStringstringWithFormat:@"%@.jpg", str];
[formData appendPartWithFileData:imageData
name:@"file"
fileName:fileName
mimeType:@"image/jpeg"];//类型根据自己需求填写
}
success:^(NSURLSessionDataTask *_Nonnull task,id _Nullable responseObject)
{
//上传成功
}
failure:^(NSURLSessionDataTask *_Nullable task,NSError * _Nonnull error)
{
//上传失败
}];