iPhone - 翻转 UIImage

发布于 2024-10-28 23:17:42 字数 1351 浏览 1 评论 0 原文

我正在开发一个使用 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°,但这由于任何神秘的原因不起作用。

所以我的问题是:你能帮我编写一个有效的优化方法吗?我将能够理解它是如何工作的

I'm working on an app that uses the front camera of the iPhone.
When an image is captured with this camera, it is wirrored horizontally by the iPhone.
I want to mirror it back to be able to save it and to display it as it was seen on the iPhone screen.

I've read lots of docs, and lots of advices on the net, and I'm still very confused.

After my researches and many tries, I've found that solution that works for both saving and displaying :

- (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;
}

But it's a blind use and I don't understand what is done.

I tried to use 2 imageWithCGImage to mirror it up then rotate it by 180°, but this don"t work for any mysterious reason.

So my question is : could you help me to write an optimised method that works, and which I would be able to understand how it works. Matrix is a black hole for me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2024-11-04 23:17:42

如果该矩阵太神秘了,则可能将其分为两个步骤使其更容易理解:

CGContextRef context = UIGraphicsGetCurrentContext();

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

[tempImageView.layer renderInContext:context];

转换矩阵从第一到最后。最初,画布被向上移动,然后否定了图像的y坐标:

            +----+
            |    |
            | A  |
+----+      o----+     o----+
|    |                 | ∀  |
| A  | -->         --> |    |
o----+                 +----+

      x=x         x=x
      y=y+h       y=-y

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

 x = x
 y = -y + h

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

x = a*x + c*y + e
y = b*x + d*y + f

see http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html for more info about核心图形中的2D仿射变换。

If that matrix is too mysterious, perhaps separating it into two steps make it easier to understand:

CGContextRef context = UIGraphicsGetCurrentContext();

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

[tempImageView.layer renderInContext:context];

Transformation matrices are applied from first to last. Initially, the canvas is moved upward, and then the image's y-coordinates are all negated:

            +----+
            |    |
            | A  |
+----+      o----+     o----+
|    |                 | ∀  |
| A  | -->         --> |    |
o----+                 +----+

      x=x         x=x
      y=y+h       y=-y

The two formulae that changes the coordinates can be combined into one:

 x = x
 y = -y + h

The CGAffineTransformMake you have made represents this. Basically, for CGAffineTransformMake(a,b,c,d,e,f), it corresponds to

x = a*x + c*y + e
y = b*x + d*y + f

See http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html for more info about 2D affine transform in Core Graphics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文