从UIImagePickerController发送UIImage POST到服务器?

时间:2014-09-23 16:40:54

标签: ios uiimagepickercontroller nsurlsession nsurlsessionuploadtask

我正在尝试使用UIImagePickerController将UIImage带到服务器POST以及其他相关值。但是我试着设置字典值@" image"到UIImageJPEGRepresentation(图片,1.0):

-(void)sendImageToServer:(UIImage *)image
{
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    queue.maxConcurrentOperationCount = 4;

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration] delegate:nil delegateQueue:queue];
    NSURL *uploadURL = [NSURL URLWithString:@"http://...."];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:uploadURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];

    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPMethod:@"POST"];

    NSData *postData = [[NSData alloc] init];
    [postData setValue:UIImageJPEGRepresentation(image, 1.0) forKey:@"image"];
    [postData setValue:@"1" forKey:@"categories[0]"];
    [postData setValue:@"4" forKey:@"categories[1]"];

    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                               fromData:postData
                                                      completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        if (!error) {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
            if (httpResponse.statusCode == 200) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSError *err;
                    NSDictionary *JSONDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&err];
                    NSLog(@"HTTP 200 response: %@", JSONDict);
                });
            } else {    
                NSLog(@"HTTP %ld status!", (long)httpResponse.statusCode);
            }   
        } else {
            NSLog(@"HTTP post image error: %@", error);
        }
    }];
    [uploadTask resume];
}

JSON序列化在这里不起作用,因为图像不是有效的JSON值。另一方面,如果我尝试:

...
NSMutableData *postData = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:postData];

    [archiver encodeObject:UIImageJPEGRepresentation(image, 1.0) forKey:@"image"];
    [archiver encodeObject:@"1" forKey:@"categories[0]"];
    [archiver encodeObject:@"4" forKey:@"categories[1]"];
    [archiver finishEncoding];



    //NSData *postData = [NSJSONSerialization dataWithJSONObject:dataDict options:NSJSONWritingPrettyPrinted error:&jsonError];
    //Now you can post the json data

    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                               fromData:postData
                                                      completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {...

密钥:归档的值对似乎没有这样到达服务器。这必须是一个例行的iOS编码任务。

即使我只是尝试:

NSError *jsonError;
    NSData *postData = [NSJSONSerialization dataWithJSONObject:@{@"image":@"123",@"categories[0]":@"1",@"categories[1]":@"4"} options:NSJSONWritingPrettyPrinted error:&jsonError];

服务器根本没有获得任何密钥......

3 个答案:

答案 0 :(得分:0)

这不是NSData的正确用法。它正在崩溃,因为NSData没有关键的命名图像(或者之后的其他两个)。您需要做的是创建NSDictionary,然后将其转换为NSData

做这样的事情:

NSDictionary *dictionary = [NSDictionary alloc]initWithObjectsAndKeys:image,@"image",@(1),@"categories[0]",@(4),@"categories[1]", nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary]; //Not currently using NSJSONSerialization since you want to post a Dictionary with an invalid NSJSONSerialization type in it.
//Now you can post the json data

答案 1 :(得分:0)

尝试使用AFNetworking,它有一个很好的上传方式,你可以在这里找到样本:https://github.com/AFNetworking/AFNetworking#creating-an-upload-task

我个人建议每个人都使用它,因为我开始使用我没有任何麻烦与网络服务器沟通我的应用程序。

答案 2 :(得分:0)

使用AFNetworking和多部分表单帖子。这是一个粗略的例子(注意我传递的是块,所以你的实现会有所不同):

AFHTTPRequestOperation *operation = [self POST:FullURLString parameters:Params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:fileData name:fileName fileName:fileName mimeType:mimeType];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *responseData = [operation responseData];
        id retObj;
        NSError *error = nil;
        if (responseData) {
            retObj = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
        }
        // Pass back the serialized object (either an NSArray of type NSDictionaries or an NSArray of type customClass)
        block(retObj);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failed with error = [Error]: %@", error);
        block(nil);
}];