###上传图片 附件流上传
- (void)saveImage:(UIImage *)image
{
NSString *url = @"";//放上传图片的网址
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];//初始化请求对象
manager.responseSerializer = [AFHTTPResponseSerializer serializer];//设置服务器允许的请求格式内容
//上传图片/文字,只能用POST
[manager POST:url parameters:nil constructingBodyWithBlock:^(id _Nonnull formData) {
//对于图片进行压缩
//UIImage *image = [UIImage imageNamed:@"111"];
NSData *data = UIImageJPEGRepresentation(image, 0.1);
//NSData *data = UIImagePNGRepresentation(image);
//第一个代表文件转换后data数据,第二个代表图片的名字//服务器有时候会要求这个参数名字一致,第三个代表图片放入文件夹的名字,第四个代表文件的类型
//多张图片 可用数组上传
[formData appendPartWithFileData:data name:@"1" fileName:@"image.jpg" mimeType:@"image/jpg"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"uploadProgress = %@",uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject = %@, task = %@",responseObject,task);
// id obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
// NSLog(@"obj = %@",obj);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@",error);
}];
}
复制代码
简单封装一下AF的网络请求
typedef void(^SuccessBlock)(id data);
typedef void(^FailBlock)(NSError *error);
//网络请求数据
+ (void)sendGETWithUrl:(NSString *)url parameters:(NSDictionary *)dict success:(SuccessBlock)successBlock fail:(FailBlock)failBlock
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:url parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
successBlock(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failBlock(error);
}];
}
+ (void)sendPOSTWithUrl:(NSString *)url parameters:(NSDictionary *)dict success:(SuccessBlock)successBlock fail:(FailBlock)failBlock
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
successBlock(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failBlock(error);
}];
}
+ (void)sendPOSTWithUrlStr:(NSString *)url parameters:(NSString *)string success:(SuccessBlock)successBlock fail:(FailBlock)failBlock{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:string progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
successBlock(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failBlock(error);
}];
}
复制代码
转载自 骑着单车的帅小伙的博客