LoginSignup
61

More than 5 years have passed since last update.

UIViewをUIImageに変換する

Last updated at Posted at 2012-12-26

UIViewに複雑なアニメーションをさせたい時など、普通にUIViewのままいじるとパフォーマンスが悪くなるため、UIImage化していじることが多い。

http://markpospesel.com/2012/05/07/mpfoldtransition/
このフレームワークとかでも、この手法を使っている。

その変換方法。

- (UIImage *)imageFromView:(UIView *)view
{
    // 必要なUIImageサイズ分のコンテキスト確保
    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // 画像化する部分の位置を調整
    CGContextTranslateCTM(context, -view.frame.origin.x, -view.frame.origin.y);

    // 画像出力
    [view.layer renderInContext:context];

    // uiimage化
    UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();

    // コンテキスト破棄
    UIGraphicsEndImageContext();

    return renderedImage;
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
61