【问题标题】:Image is rotating when sending only in multipart post request to the backend server仅在多部分发布请求中向后端服务器发送图像时,图像正在旋转
【发布时间】: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


【解决方案1】:

您应该在上传到服务器之前从 UIImage 中删除方向数据。

UIImage *originalImage = [requestBody objectForKey:keyUploadImage];

UIImage *imageToUpload =
[UIImage imageWithCGImage:[originalImage CGImage]
                    scale:[originalImage scale]
              orientation: UIImageOrientationUp];

您应该使用 imageToUpload 上传到服务器。

【讨论】:

  • 您能以某种方式发送指向原始图像和服务器图像的链接吗?
  • 我的图像方向已经是向上的,但是在上传后它会旋转,并且只有在我裁剪图像时才会发生这种情况
  • 裁剪?你在哪里裁剪图像,我在你的代码中看不到它。
  • 在此之前我正在做的作物
  • 我一开始就是这么告诉你的。而且您使用的不是 jpeg 而是 png 格式,它不支持图像方向。
【解决方案2】:

当我裁剪图像时发生图像旋转,然后我在裁剪后使用了这个https://stackoverflow.com/a/14231694/7636194,然后我的问题得到了解决。

【讨论】:

    猜你喜欢
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2019-12-24
    • 2016-02-27
    • 2018-09-16
    相关资源
    最近更新 更多