iPhone - 翻转UIImage

Oli*_*ver 2 iphone graphics camera mirror flip

我正在开发一款使用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;
}
Run Code Online (Sandbox Code Playgroud)

但这是盲目使用,我不明白做了什么.

我尝试使用2个imageWithCGImage将其镜像,然后将其旋转180°,但这不能用于任何神秘的原因.

所以我的问题是:你能帮助我编写一个有效的优化方法,并且我能够理解它是如何工作的.矩阵对我来说是一个黑洞......

ken*_*ytm 10

如果那个矩阵太神秘,可能将它分成两个步骤使它更容易理解:

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0, tempImageView.frame.size.height);
CGContextScaleCTM(context, 1, -1);

[tempImageView.layer renderInContext:context];
Run Code Online (Sandbox Code Playgroud)

转换矩阵从头到尾应用.最初,画布向上移动,然后图像的y坐标全部被否定:

            +----+
            |    |
            | A  |
+----+      o----+     o----+
|    |                 | ?  |
| A  | -->         --> |    |
o----+                 +----+

      x=x         x=x
      y=y+h       y=-y
Run Code Online (Sandbox Code Playgroud)

改变坐标的两个公式可以组合成一个:

 x = x
 y = -y + h
Run Code Online (Sandbox Code Playgroud)

您制作的CGAffineTransformMake代表了这一点.基本上,因为CGAffineTransformMake(a,b,c,d,e,f),它对应于

x = a*x + c*y + e
y = b*x + d*y + f
Run Code Online (Sandbox Code Playgroud)

有关Core Graphics中2D仿射变换的更多信息,请参见http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html.