2

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°回転させようとしましたが、不思議な理由で機能しません。

だから私の質問は:私が機能する最適化されたメソッドを書くのを手伝ってくれませんか、そしてそれがどのように機能するかを理解することができます。マトリックスは私にとってブラックホールです...

4

1 に答える 1

10

その行列が不思議すぎる場合は、おそらく2つのステップに分けると、理解しやすくなります。

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

座標を変更する2つの式を1つに組み合わせることができます。

 x = x
 y = -y + h

あなたが作ったCGAffineTransformMakeはこれを表しています。基本的に、の場合CGAffineTransformMake(a,b,c,d,e,f)、に対応します

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

コアグラフィックスの2Dアフィン変換の詳細については、http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.htmlを参照してください。

于 2011-04-03T22:16:49.937 に答える