我一直在寻找新的AFNetworking 2.0上传图像的例子.
但是我正在撞墙,无法弄清楚代码有什么问题.
所以这是我使用的代码
但是我正在撞墙,无法弄清楚代码有什么问题.
所以这是我使用的代码
NSData *imageData = UIImageJPEGRepresentation(image,0.5); NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; NSURL *URL = [NSURL URLWithString:@"http://myserverurl.com"]; NSURLRequest *request = [NSURLRequest requestWithURL:URL]; NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromData:imageData progress:nil completionHandler:^(NSURLResponse *response,id responSEObject,NSError *error) { if (error) { NSLog(@"Error: %@",error); } else { NSLog(@"Success: %@ %@",response,responSEObject); } }]; [uploadTask resume];
TIA
解决方法
我最终使用了多部分请求
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(image,0.5); AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; NSDictionary *parameters = @{@"foo": @"bar"}; [manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { [formData appendPartWithFormData:imageData name:@"image"]; } success:^(AFHTTPRequestOperation *operation,id responSEObject) { NSLog(@"Success: %@",responSEObject); } failure:^(AFHTTPRequestOperation *operation,NSError *error) { NSLog(@"Error: %@",error); }];