如何直接从iPad应用程序使用AFNetworking上传图像

时间:2015-02-25 10:05:13

标签: ios ipad afnetworking

我是iOS新手。我想使用 AFNetworking 将图片上传到我的服务器。 我试了但是还没完成。请帮助我,提前谢谢

1 个答案:

答案 0 :(得分:4)

尝试使用它为我工作

-(void) uplodeImages{

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager   alloc] initWithBaseURL:[NSURL URLWithString:@"url where to upload images"]];
    NSData *imageData = UIImagePNGRepresentation([UIImage    imageNamed:@"cat1.png"]);
    NSDictionary *parameters = @{@"username": @"", @"password" : @""};
    AFHTTPRequestOperation *op = [manager POST:@"cat1.png" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        //do not put image inside parameters dictionary as I did, but append it!
        [formData appendPartWithFileData:imageData name:@"userfile" fileName:@"cat1.png" mimeType:@"image/png"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@ ***** %@", operation.responseString, error);
    }];
    [op start];
}