如何使用字典iOS在服务器上发布图像?

时间:2016-04-20 13:15:20

标签: ios objective-c uiimage http-post nsdictionary

我有else个键/值和dictionary (dict)。现在我想用图书将图像上传到服务器。我已经尝试过但没有取得任何成功。这是我的示例代码 -

image

我在哪里出错了我需要帮助..提前致谢

2 个答案:

答案 0 :(得分:1)

试用以下代码:

    -(void)uploadImage:(NSString *)api :(NSDictionary *)params : (UIImage *)image {

    NSURL *myURL = [NSURL URLWithString:api];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:myURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@; charset=UTF-8", boundary];
    [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
    NSMutableData *theBodyData = [NSMutableData data];
    for (int i=0; i<[[params allKeys] count]; i++)
    {
        [theBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        NSString *value=@"";
        value=[params objectForKey:[[params allKeys] objectAtIndex:i]];
        NSString * value1=[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n%@\r\n",[[params allKeys] objectAtIndex:i],value];
        [theBodyData appendData:[value1 dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [theBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [theBodyData appendData:[@"Content-Disposition: form-data; name=\"my_file1\"; filename=\"image1.jpeg\"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];

    [theBodyData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
    NSData *myData = UIImageJPEGRepresentation(image,0.5);
    [theBodyData appendData:[NSData dataWithData:myData]];

    [theBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];



    [request setHTTPBody:theBodyData];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", (int)[theBodyData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: theBodyData];

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

    NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if(error == nil) {
            NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Server Raw Response : %@",responseString);
        }

    }];

    [dataTask resume];


}

答案 1 :(得分:0)

您的代码看起来不错,但我认为您必须将字典转换为json string。只需替换此行 -

[body appendData:[[NSString stringWithFormat:@"%@",dict] dataUsingEncoding:NSUTF8StringEncoding]];

NSString *jsonStr = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];

[body appendData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];

可能会对你有帮助。