11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画像の一部を切り取って使う

Last updated at Posted at 2014-02-13

$Actor_etc2.png

こういうスプライト画像の

20140213-235211.png

この部分だけ切り取る時とかに使います。

UIImage+Custom.m
#import "UIImage+Custom.h"

@implementation UIImage (Custom)

- (UIImage *)partialImageOfRect:(CGRect)rect
{
	CGPoint	originDrawPoint	= CGPointMake(rect.origin.x * -1, rect.origin.y * -1);
	
	UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
	[self drawAtPoint:originDrawPoint];
	UIImage* partialImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return partialImage;
}

@end

使う時は

// 元のスプライト画像を取得
UIImage*  sourceImage = [UIImage imageNamed:@"tono.png"];
// 切り取る枠を指定
CCRect    rect  = CGRectMake(0, 0, 32, 32);
// トリミング画像を取得
UIImage*  createdImage = [sourceImage partialImageOfRect:rect];

こんな感じ。

もっと良いやり方があったら教えて下さい。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?