通过AFNetworking 2.0上传iOS图像

前端之家收集整理的这篇文章主要介绍了通过AFNetworking 2.0上传iOS图像前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在寻找新的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);
}];
原文链接:/iOS/331302.html

猜你在找的iOS相关文章