1

UIScrollView内にUIImageViewがあり、ユーザーが任意の数の反転および回転操作を実行できるようにしています。私はこれをすべて機能させており、ユーザーはズーム、パン、フリップ、回転を行うことができます。ここで、最終的な画像をpngに保存できるようにしたいと思います。

しかし、これを解決しようと頭を悩ませています...

これに似た他の投稿をかなり多く見ましたが、ほとんどの場合、回転などの単一の変換を適用するだけで済みます。たとえば、回転したUIImageViewからUIImageを作成します。

ユーザーが「作成」した変換を適用したいと思います。これは、一連の反転と回転が連結されたものになります。

ユーザーがさまざまな回転や反転などを適用しているので、CGAffineTransformConcatを使用して連結された変換を保存します。たとえば、彼らが回転するとき、私はします:

CGAffineTransform newTransform = CGAffineTransformMakeRotation(angle);
self.theFullTransform = CGAffineTransformConcat(self.theFullTransform, newTransform);
self.fullPhotoImageView.transform = self.theFullTransform;

次の方法は、完全な変換を使用してUIImageを作成するためにこれまでに得た最良の方法ですが、画像は常に間違った場所に変換されます。たとえば、画像は「オフセット」です。私の推測では、CGAffineTransformTranslateまたはCGContextDrawImageで設定されている間違った境界を使用することに関連しています。

誰かアイデアはありますか?これは私がそうあるべきだと思ったよりもずっと難しいようです...

- (UIImage *) translateImageFromImageView: (UIImageView *) imageView withTransform:(CGAffineTransform) aTransform
{
    UIImage *rotatedImage;

    // Get image width, height of the bounding rectangle
    CGRect boundingRect = CGRectApplyAffineTransform(imageView.bounds, aTransform);

    // Create a graphics context the size of the bounding rectangle
    UIGraphicsBeginImageContext(boundingRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGAffineTransform transform = CGAffineTransformIdentity;

    //I think this translaton is the problem?

    transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    transform = CGAffineTransformConcat(transform, aTransform);

    CGContextConcatCTM(context, transform);

    // Draw the image into the context

    // or the boundingRect is incorrect here?

    CGContextDrawImage(context, boundingRect, imageView.image.CGImage);

    // Get an image from the context
    rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];

    // Clean up
    UIGraphicsEndImageContext();
    return rotatedImage;
}
4

1 に答える 1

1

常に画像の半分のようにオフセットは予測可能ですか、それともaTransformに依存しますか?

struct CGAffineTransform {
  CGFloat a, b, c, d;
  CGFloat tx, ty;
};

後者の場合は、使用する前にaTransformでゼロに設定してくださいtxty

于 2010-06-25T04:25:34.533 に答える