仅向后端服务器发送 multipart post 请求时图像正在旋转
Image is rotating when sending only in multipart post request to the backend server
我正在使用多部分表单数据发出 post 请求。使用以下代码我可以发送图像但是当我去后端服务器检查时,我发送的图像在那里旋转。任何人都知道为什么会发生?
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 --- 这是我想要的实际图像
在上传到服务器之前,您应该从 UIImage 中删除方向数据。
UIImage *originalImage = [requestBody objectForKey:keyUploadImage];
UIImage *imageToUpload =
[UIImage imageWithCGImage:[originalImage CGImage]
scale:[originalImage scale]
orientation: UIImageOrientationUp];
您应该使用 imageToUpload 上传到服务器。
当我裁剪图像时发生图像旋转,然后我在裁剪后使用这个 然后我的问题得到解决。
我正在使用多部分表单数据发出 post 请求。使用以下代码我可以发送图像但是当我去后端服务器检查时,我发送的图像在那里旋转。任何人都知道为什么会发生?
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 --- 这是我想要的实际图像
在上传到服务器之前,您应该从 UIImage 中删除方向数据。
UIImage *originalImage = [requestBody objectForKey:keyUploadImage];
UIImage *imageToUpload =
[UIImage imageWithCGImage:[originalImage CGImage]
scale:[originalImage scale]
orientation: UIImageOrientationUp];
您应该使用 imageToUpload 上传到服务器。
当我裁剪图像时发生图像旋转,然后我在裁剪后使用这个 然后我的问题得到解决。