【发布时间】:2018-07-02 08:49:05
【问题描述】:
我正在使用多部分表单数据进行发布请求。使用以下代码,我可以发送图像,但是当我去后端服务器检查时,我发送的图像在那里旋转。有人知道为什么会这样吗?
NSURL *reqUrl = [NSURL URLWithString:self.dataModel.apiUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:reqUrl];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request setHTTPMethod:@"POST"];
NSDictionary *requestBody = self.dataModel.requestParams2;
UIImage *imageToUpload = [requestBody objectForKey:keyUploadImage];
if(requestBody){
NSMutableData *body = [NSMutableData data];
float low_bound = 0;
float high_bound =5000;
float rndValue = (((float)arc4random()/0x100000000)*(high_bound-low_bound)+low_bound);//image1
int intRndValue = (int)(rndValue + 0.5);
NSString *str_image1 = [@(intRndValue) stringValue];
NSData *imageData = UIImageJPEGRepresentation(imageToUpload, 0);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile1\"; filename=\"%@.jpeg\"\n",str_image1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
}
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSLog(@"completionHandler with response:%@",[NSHTTPURLResponse localizedStringForStatusCode:[(NSHTTPURLResponse*)response statusCode]]);
NSLog(@"reponse: %ld",(long)[(NSHTTPURLResponse*)response statusCode]);
NSInteger status = [(NSHTTPURLResponse*)response statusCode];
if(error){
NSLog(@"http request error: %@", error.localizedDescription);
// handle the error
}
else{
if (status == 200){
// handle the success
}
else{
NSLog(@"error");
} // handle the error
}
}];
[1]:https://i.stack.imgur.com/FAMJy.png ---这是服务器中的图片
[1]: https://i.stack.imgur.com/7NGSK.png --- 这是我想要的实际图像
【问题讨论】:
-
@AndreasOetjen 请清楚阅读我的问题,这里我使用多部分发送请求,但不使用图像选择器控制器。只有在多部分图像旋转的情况下,它才能正常发布请求
-
我读过这个,但我认为原始 JPEG 图像以某种方式旋转。 JPEG 将图像方向存储在 EXIF 元数据中,但 PNG 不存储。要进行测试,您可以将 PNG 存储到本地文件系统,看看它是否也“错误地”旋转。或者,只是将 PNG 而不是 JPEG 发送到服务器。
-
我的后端服务器只接受 jpeg 格式为此做什么,并且相同的 jpeg 图像数据在 post 请求中正确处理
-
也许我看错了。不过,您应该检查字体和后端的 exif 数据中的方向标志
-
但是如果没有正确阅读,你怎么能评论为重复呢??奇怪
标签: ios objective-c post multipartform-data image-rotation