iOS UIImagePickerController 上传后结果图像方向
我正在 iOS 3.1.3 iPhone 上测试我的 iPhone 应用程序。我正在使用 UIImagePickerController
选择/捕获图像:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self.navigationController presentModalViewController:imagePicker animated:YES];
[imagePicker release];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = self.image;
[self.navigationController dismissModalViewControllerAnimated:YES];
submitButton.enabled = YES;
}
然后我在某个时候使用 ASI 类将其发送到我的网络服务器:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://example.com/myscript.php"]];
[request setDelegate:self];
[request setStringEncoding:NSUTF8StringEncoding];
[request setShouldContinueWhenAppEntersBackground:YES];
//other post keys/values
[request setFile:UIImageJPEGRepresentation(self.image, 100.0f) withFileName:[NSString stringWithFormat:@"%d.jpg", [[NSDate date] timeIntervalSinceNow]] andContentType:@"image/jpg" forKey:@"imageFile"];
[request startAsynchronous];
问题: 当我用iPhone横着拍照时,图像会上传到服务器,并且看起来就像你所期望的那样。手持手机纵向拍照时,上传并查看的图像会旋转 90 度。
我的应用程序设置为仅在纵向模式下工作(颠倒和常规)。
如何使图片上传后始终显示正确的方向?
图像在 UIImageView 中显示时似乎是正确的(拍照后立即),但在服务器上查看却不然。
I am testing my iPhone application on an iOS 3.1.3 iPhone. I am selecting/capturing an image using a UIImagePickerController
:
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate:self];
[self.navigationController presentModalViewController:imagePicker animated:YES];
[imagePicker release];
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = self.image;
[self.navigationController dismissModalViewControllerAnimated:YES];
submitButton.enabled = YES;
}
I then at some point send it to my web server using the ASI classes:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://example.com/myscript.php"]];
[request setDelegate:self];
[request setStringEncoding:NSUTF8StringEncoding];
[request setShouldContinueWhenAppEntersBackground:YES];
//other post keys/values
[request setFile:UIImageJPEGRepresentation(self.image, 100.0f) withFileName:[NSString stringWithFormat:@"%d.jpg", [[NSDate date] timeIntervalSinceNow]] andContentType:@"image/jpg" forKey:@"imageFile"];
[request startAsynchronous];
the problem:
when i take a picture with the iphone while holding it landscape, the image gets uploaded to the server and it viewed like you would expect. when taking a picture holding the phone in portrait, the image is uploaded and viewed as it had been rotated 90 degrees.
my application is set to only work in portrait modes(upsidedown and regular).
How can i make the image always show the correct orientation after uploading?
the image appears to be correct as displayed in an UIImageView(directly after taking the picture), but viewing on the server says otherwise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
这是自动修复图像方向的 Swift-4.2 代码
返回 UIImage
Here is the Swift-4.2 code for automatic fix your image orientation
Returns UIImage
我将其转换为 Xamarin:
I transposed this into Xamarin:
UIImage
具有属性imageOrientation
,它指示UIImageView
和其他UIImage
使用者旋转原始图像数据。该标志很有可能被保存到上传的 jpeg 图像中的 exif 数据中,但您用来查看它的程序不支持该标志。要旋转
UIImage
以在上传时正确显示,您可以使用如下类别:UIImage+fixOrientation.h
UIImage+fixOrientation.m
A
UIImage
has a propertyimageOrientation
, which instructs theUIImageView
and otherUIImage
consumers to rotate the raw image data. There's a good chance that this flag is being saved to the exif data in the uploaded jpeg image, but the program you use to view it is not honoring that flag.To rotate the
UIImage
to display properly when uploaded, you can use a category like this:UIImage+fixOrientation.h
UIImage+fixOrientation.m
我想出了一个更简单的方法:
顺便说一句:@Anomie 的代码没有考虑
scale
,因此不适用于 2x 图像。I figured out a much simpler one:
BTW: @Anomie's code does not take
scale
into account, so will not work for 2x images.这是 @an0 的答案的 Swift 版本:
也在更通用的函数中:
Swift 3 版本:
Here is a Swift version of the answer by @an0:
Also in a more general function:
Swift 3 version:
Swift 3.1从相机捕获图像时出现方向问题的解决方案。
我已经更新了jake和Metal Heart提供的解决方案
UIImage扩展
Swift 2.0
在代码中使用此 UIImage 扩展:
let fixOrientationImage=chosenImage.fixOrientation()
将其放置在图像选择器的委托方法中,如下所示
Swift 3.1
Swift 2.0< /强>
Solution for Swift 3.1 for orientation issue while capturing the image from Camera.
I have updated the solution given by jake and Metal Heart
UIImage extension
Swift 2.0
Use of this UIImage Extension in your code:
let fixOrientationImage=chosenImage.fixOrientation()
place this in your delegate methods of image picker like this
Swift 3.1
Swift 2.0
Swift 4.x/5.0 版本
@an0
的解决方案:Swift 4.x/5.0 version of
@an0
's solution:迅速更新 ;)
更新 SWIFT 3.0 :D
in swift ;)
UPDATE SWIFT 3.0 :D
我在设计拍照应用程序时使用了此页面,我发现以下方法将纠正方向并使用比以前的答案更少的内存和处理器:
这基本上只是用新方向重新包装实际图像数据。我使用的是 @an0 的代码,但它会在内存中生成一个新图像,这可能会对您从相机获取的 3264x2448 图像造成负担。
I used this page when designing my app that takes pictures and I found that the following method will correct the orientation and use less memory and processor than previous answers:
This basically just rewraps the actual image data with a new orientation. I was using @an0's code but it makes a new image in memory which can be taxing on a 3264x2448 image that you might get from a camera.
如果启用编辑,则编辑后的图像(与原始图像相反)将按预期定向:
启用编辑允许用户在点击“使用照片”之前调整图像大小和移动图像
If you enable editing, then the edited image (as opposed to the original) will be oriented as expected:
Enabling editing allows the user to resize and move the image before tapping "Use Photo"
我通过写下面几行代码来实现这一点
I achieve this by writing below a few lines of code
这是我发现的解决方向问题的方法
编辑:
This what I have found for fixing orientation issue
EDIT:
这是一个不会更改原始图像的色彩空间的解决方案。如果您想要规范化灰度图像的方向,那么所有基于
UIGraphicsBeginImageContextWithOptions
的解决方案都不走运,因为它会在 RGB 颜色空间中创建上下文。相反,您必须创建一个与原始图像具有相同属性的上下文并进行绘制:Here’s a solution that doesn’t change the colorspace of the original image. If you want to normalize the orientation of a grayscale image, you are out of luck with all solutions based on
UIGraphicsBeginImageContextWithOptions
because it creates a context in the RGB colorspace. Instead, you have to create a context with the same properties as the original image and draw:基于
Sourabh Sharma
的答案更新 Swift 3.1,并进行代码清理。选择器委托方法示例:
Update for Swift 3.1 based on
Sourabh Sharma
's answer, with code clean up.Picker delegate method example:
这是 swift 的 UIImage 扩展:
基于 MetalHeart2003 的早期工作..
Here's UIImage extension for swift:
Based on MetalHeart2003's earlier work..
这是 Swift 2 中的 UIImage 扩展,基于 @Anomie 接受的答案。它使用了更清晰的开关盒。它还考虑了 CGBitmapContextCreateImage() 返回的可选值。
Here is an UIImage extension in Swift 2 based on the accepted answer by @Anomie. It uses a clearer switch case. It also takes the optional value returned by
CGBitmapContextCreateImage()
into consideration.我在使用从相机拍摄的图像或保存在相机胶卷中的图像时遇到过此问题。从 safari 浏览器下载到照片库中的图像在上传时不会旋转。
我通过在上传之前将图像数据设为 JPEG 来解决这个问题。
现在我们可以使用数据进行上传,并且上传后图像不会旋转。
希望这会起作用。
I have experienced this issue with images taken from camera or saved in camera roll which are taken from camera. Images downloaded in photo library from safari browser does not rotate when uploaded.
I was able to solve this issue by making the image data as JPEG before uploading.
We can now use the data for uploading and the image will not get rotated after upload.
Hope this will work.
如果我理解的话,你想要做的是忽略 UIImage 的方向?如果是这样,那么你可以这样做:-
或在 Swift 中:-
它解决了我的裁剪问题..希望,这就是你正在寻找的..
If I understand, what you want to do is disregard the orientation of the UIImage? If so then you could do this:-
or in Swift :-
It solved my cropping issue.. Hope, this is what you're looking for..
这是 Swift-5 或更高版本的代码,用于自动修复您的图像方向返回 UIImage。
Here is the Swift-5 or later version code for automatic fix your image orientation Returns UIImage.
Swift 3 版本基于 @jake1981,他从 @MetalHeart2003 获取它
Swift 3 version based on @jake1981 who've taken it from @MetalHeart2003