LoginSignup
12
11

More than 5 years have passed since last update.

UIImageの合成

Last updated at Posted at 2014-10-31

ある入力フォーム(UIImage)にたいしてUILabelを貼付けたい。
UIImageとUIImageを合成するのが簡単らしい。
※UIImageViewにUILabelをaddしてもだめだった。

段階1) UIView(UILabel)のUIImage化

//! viewLabelsはclearカラー、複数のUILabelが乗っかっている
//! opaque引数をNOにすることに注意
UIGraphicsBeginImageContextWithOptions(viewLabels.bounds.size, NO, 0.0); // opaqueをNOにして背景をクリアにする
[viewLabels.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *imageFromUIView = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

段階2) UIImageの合成

// グラフィックスコンテキストを作る
CGSize size = { _imageOriginal.size.width, _imageOriginal.size.height};
UIGraphicsBeginImageContext(size);

//元画像を描画
CGRect rect;
rect.origin = CGPointZero;
rect.size = size;
[_imageOriginal drawInRect:rect];

//重ね合わせる画像を描画
[imageFromUIView drawInRect:rect];

// 合成した画像を取得する
UIImage* compositeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
12
11
0

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
12
11