iPhone - 镜像回从前置摄像头拍摄的照片
使用 iPhone 4 的前置摄像头拍照时,拍摄的照片与 iPhone 屏幕上看到的图像是镜像的。我如何恢复 UIImage (而不是 UIImageView)的“屏幕上”视图,并能够像这样保存它?
我尝试过:
UIImage* transformedImage = [UIImage imageWithCGImage:pickedImage.CGImage scale:1.0 orientation:UIImageOrientationLeftMirrored];
UIImageWriteToSavedPhotosAlbum (transformedImage, self, @selector(photoSaved:didFinishSavingWithError:contextInfo:), nil);
然后将其放在屏幕上。它与屏幕上看到的几乎相同,但保存的图像失真。
那么...我如何恢复 UIImage (而不是 UIImageView)的“屏幕”视图,并能够像这样保存它?
我也尝试过这种方式:
UIImage* pickedImage = [[info objectForKey:UIImagePickerControllerOriginalImage] retain];
UIImage* transformedImage;
CGSize imageSize = pickedImage.size;
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1.0);
GContextRef ctx = UIGraphicsGetCurrentContext();
CGContextRotateCTM(ctx, 3.14); // rotate by 180°
CGContextScaleCTM(ctx, 1.0, -1.0); // flip vertical
CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, imageSize.width, imageSize.height), pickedImage.CGImage);
transformedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
但这只是给出了一个黑色图像。
When using the front camera of the iPhone 4 to take a picture, the taken picture is mirrored compared with what you see on the iPhone screen. How may I restore the "on screen" view of the UIImage (not the UIImageView), and be able to save it like this ?
I tried :
UIImage* transformedImage = [UIImage imageWithCGImage:pickedImage.CGImage scale:1.0 orientation:UIImageOrientationLeftMirrored];
UIImageWriteToSavedPhotosAlbum (transformedImage, self, @selector(photoSaved:didFinishSavingWithError:contextInfo:), nil);
then putting it on screen. It is nearly the same as seen on screen, but the saved image is distorted.
So... How may I restore the "on screen" view of the UIImage (not the UIImageView), and be able to save it like this ?
I also tried this way :
UIImage* pickedImage = [[info objectForKey:UIImagePickerControllerOriginalImage] retain];
UIImage* transformedImage;
CGSize imageSize = pickedImage.size;
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1.0);
GContextRef ctx = UIGraphicsGetCurrentContext();
CGContextRotateCTM(ctx, 3.14); // rotate by 180°
CGContextScaleCTM(ctx, 1.0, -1.0); // flip vertical
CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, imageSize.width, imageSize.height), pickedImage.CGImage);
transformedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
But that just gives a black image.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我知道这个问题已经得到解答,但上面的答案对我不起作用,所以万一还有其他问题......
newImage 是您的新图像,方向向上
I know this question was already answered, but the answer above didn't work for me so in case there are others...
and newImage is your new image with the up orientation
最好的方法是将图像绘制到新的上下文中。
这是在内存不足的情况下编写的,没有编译器的帮助,请不要复制和粘贴......
The best way is to draw the image into a new context.
This was written out of memory and without aid of a compiler, please don't copy and paste...
您可能想要镜像从相机获得的图像,但您最初获得的图像是正确的。拍一张带有文字的图片进行比较。
You might want to mirror the image you get from the camera but what you originally get is correct. Take a picture with text to compare.
安德鲁·帕克的回答非常有效。这是斯威夫特版本。
Andrew Park's answer works great. This is Swift version.
为 Swift 3 更新了一个稍微简单的答案:
A slightly simpler answer updated for Swift 3:
正如其他答案,我也遇到了同样的问题。但仅仅翻转最终图像只是答案的一半,因为当您使用前置摄像头拍照时 UIPickerController 显示的预览图像仍然是反转的(镜像)。
根据互联网上的一些代码,我创建了一个 Pod 来获得这种想要的行为:
https://github.com/lucasecf/ LEMirroredImagePicker
安装后,你只需要将这两行代码与你的
UIImagePickerController
一起调用即可:就是这样,就这么简单。您可以在 github 链接的 README 中查看更多信息。
As the other answers, I had the same problem. But just flip the final image is only half the answer, because the preview image, displayed by the UIPickerController when you take a picture with the front camera, it's still inverted (mirrored).
Based in some codes from internet, I created a Pod to get this wanted behavior:
https://github.com/lucasecf/LEMirroredImagePicker
After installed, you just have to call this two lines of code together with your
UIImagePickerController
:And thats it, simply as that. You can check for more informations in the README of the github link.
我想投票@Lucas Eduardo,我尝试将组件 LEImagePickerController 集成到我的项目中,如下所示:
I'd like to vote up @Lucas Eduardo , I have tried to integrate the component LEImagePickerController into my project as below: