iOS Image upload via AFNetworking 2.0

后端 未结 1 472
灰色年华
灰色年华 2020-11-29 02:53

I\'ve been looking examples for the new AFNetworking 2.0 to upload images. But I\'m hitting wall and couldn\'t figure out what\'s wrong with the code. So this is the code I

相关标签:
1条回答
  • 2020-11-29 03:51

    I ended up using the multi-part request

    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);
    }];
    
    0 讨论(0)
提交回复
热议问题