我正在开发一个使用 iPhone 前置摄像头的应用程序。当使用此相机拍摄图像时,iPhone 会水平旋转图像。我想把它镜像回来,以便能够保存它并像在 iPhone 屏幕上看到的那样显示它。
我在网上阅读了很多文档和很多建议,但我仍然很困惑。
经过我的研究和多次尝试,我发现该解决方案适用于保存和显示:
- (UIImage *) flipImageLeftRight:(UIImage *)originalImage {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];
UIGraphicsBeginImageContext(tempImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform flipVertical = CGAffineTransformMake(
1, 0,
0, -1,
0, tempImageView.frame.size.height
);
CGContextConcatCTM(context, flipVertical);
[tempImageView.layer renderInContext:context];
UIImage *flipedImage = UIGraphicsGetImageFromCurrentImageContext();
flipedImage = [UIImage imageWithCGImage:flipedImage.CGImage scale:1.0 orientation:UIImageOrientationDown];
UIGraphicsEndImageContext();
[tempImageView release];
return flipedImage;
}
但这是盲目使用,我不明白做了什么。
我尝试使用 2 imageWithCGImage 将其镜像起来,然后将其旋转 180°,但出于任何神秘原因,这不起作用。
所以我的问题是:你能帮我写一个有效的优化方法,而且我能理解它是如何工作的。矩阵对我来说是一个黑洞......