我正试图将带有UIImagePickerController的UIImage take与其他相关值一起发送到服务器POST。但我看到了试图将字典值@"image"设置为UIImageJPEGRepresentation(image,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];
服务器根本没有得到任何密钥。。。
这不是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
试试AFNetworking,它有一个很好的上传方法,你可以在这里找到示例:https://github.com/AFNetworking/AFNetworking#creating-上传任务
我个人建议每个人都使用它,因为我开始使用它,所以在与网络服务器通信时没有遇到任何问题。
使用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);
}];