5
5

More than 5 years have passed since last update.

CGLayerの使い方まとめ

Posted at

CGLayerはオフスクリーンバッファとして使えるオブジェクト。同じ画像を何度も描画したい時などに便利、かつパフォーマンスもいい(らしい)。

CGLayerの生成

CGLayerのインスタンスを作るにはCGLayerCreateWithContext()を使う。

CGLayerRef CGLayerCreateWithContext(CGContextRef context, CGSize size, CFDictionaryRef auxiliaryInfo)

contextにはCGLayerが作成するバッファの雛形となるCGContextオブジェクト、通常はウィンドウのグラフィックスコンテキストを渡す。特別な設定を必要としない限り、新たに作る必要はない。
sizeはその名の通りCGLayerの大きさを指定する。
auxiliaryInfoは今のところNULLを渡しておけ、とリファレンスに書いてある。

CGLayerの編集

CGLayerはそれぞれのグラフィックスコンテキストを持っているので、それに直接描画していくことでCGLayerの中身をいじくることができる。
グラフィックスコンテキストの取得にはCGLayerGetContext()を使う。

CGContextRef CGLayerGetContext(CGLayerRef layer)

取得したグラフィックスコンテキストは煮るなり焼くなり……
ただし、sizeで指定した領域内しか描画出来ない。はみ出してしまうとその部分が切れてしまう。

CGLayerの描画

作成したCGLayerは次の関数で描画出来る。

void CGContextDrawLayerInRect(CGContextRef context, CGRect rect, CGLayerRef layer);
void CGContextDrawLayerAtPoint(CGContextRef context, CGPoint point, CGLayerRef layer);

読んで字のごとし、というのか。見たまんま。

参照:
Quartz 2D Programming Guide: Core Graphics Layer Drawing
CGLayer Reference

5
5
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
5
5