LoginSignup
20
18

More than 5 years have passed since last update.

画像を左に90°回転させる5ステップ

Last updated at Posted at 2012-12-06

1. 画像を描画するためのコンテキストを作成して取得します。

// image が元画像(UIImage)
// 元画像の縦が横向きになるので横と縦を反転して指定する
UIGraphicsBeginImageContext(CGSizeMake(image.size.height, image.size.width));
CGContextRef context = UIGraphicsGetCurrentContext();
  • 描画の開始位置は左下から描画方向 X, Y が標準になります。

2. 描画開始位置を右上に変更します。

//  image.size.height, 
//  image.size.width の場所に描画開始位置を変更します。
CGContextTranslateCTM(context, image.size.height, image.size.width);

3. 描画方向のY軸を反転します。

// X軸はそのまま(1.0)、Y軸は反転(-1.0)
CGContextScaleCTM(context, 1.0, -1.0);

4. 描画方向を90°回転します。

// M_PI は定義されていて180°を表します。
CGContextRotateCTM(context, M_PI / 2);

5. 画像をコンテキストに描画します。

// 元画像のコアグラフィックを取得します。
CGImageRef image_ref = [image CGImage];
// 領域は元画像全体になります。
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
// 元画像のコアグラフィック全体をコンテキストに描画します。
CGContextDrawImage(context, rect, image_ref);

ここまでで左に90°回転させることができます。
これを応用すれば180°回転させたり、反転させることもできます。

20
18
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
20
18