[英]UIImageOrientation/UIImagePickerController
I am using the Camera to take pictures in an IOS app. 我正在使用相机在IOS应用中拍照。 It works, the only issue I have is when I try to figure out how the user was holding the device when taking the picture.
它起作用了,我唯一的问题是当我尝试弄清楚用户在拍照时如何握住设备。
It would be much better if I could display the images in a proper way, not upside down or rotated 90 degrees. 如果能以适当的方式显示图像,而不是上下颠倒或旋转90度,那会更好。
Looking for a solution leads me to the fact that I should use the imageOrientation of UIImage but in reality: 寻找解决方案使我得出以下事实:我应该使用UIImage的imageOrientation,但实际上:
theImage.imageOrientation is always equal to UIImageOrientationUp
What am I missing? 我想念什么?
I did various research and experiment but nothing changes. 我做了各种研究和实验,但没有任何变化。 I am not showing any more code at this point, for the simple reason, I do not know which part to show to help find the problem.
由于简单的原因,我现在不显示任何代码,因为我不知道显示哪个部分来帮助发现问题。
Since I was able to find a solution on my own. 因为我能够自己找到解决方案。 I put it here, in case it may be useful to someone else.
我把它放在这里,以防它可能对其他人有用。
Looking at the value of the "imageOrientation" field on the saved image (like I was doing), indeed for some reason always shows up as UIImageOrientationUp. 查看保存的图像上的“ imageOrientation”字段的值(就像我之前所做的那样),实际上由于某种原因,它总是显示为UIImageOrientationUp。 On the other hand looking at it inside the imagePickerController:didFinishPickingMediaWithInfo: is much more meaningful.
另一方面,在imagePickerController:didFinishPickingMediaWithInfo:内部查看它更有意义。
After some more research and trials, I ended up with a method looking like that: 经过更多的研究和试验,我最终得到了一种类似于以下的方法:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
……
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
……
if ((image.imageOrientation==UIImageOrientationLeft)||
(image.imageOrientation==UIImageOrientationRight)||
(image.imageOrientation==UIImageOrientationDown)) {
UIGraphicsBeginImageContext(image.size);
[image drawAtPoint:CGPointMake(0.0, 0.0)];
image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
NSData *mediaData = UIImagePNGRepresentation(image);
……
}
And then my picture shows properly oriented, when I want to use it later in my app. 然后,当我以后要在应用程序中使用它时,我的图片将显示正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.