LoginSignup
60
59

More than 5 years have passed since last update.

GPUを使って画像のリサイズ

Posted at

CIImageのimageByApplyingTransform:を使うと、GPUを利用した画像のリサイズ処理ができます。CIContextを作るときのオプションで、CPUかGPUか処理を選べます。CoreImage.frameworkが必要。

// ソース画像を準備
UIImage *sourceA = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"IMG_0001" ofType:@"png"]];
CIImage *sourceImage = [[CIImage alloc] initWithCGImage:sourceA.CGImage];

// 新しい画像サイズ
CGSize newSize = CGSizeMake(320, 480);

// ソーズ画像のサイズと、新しいサイズの比率計算
CGRect imageRect = [sourceImage extent];
CGPoint scale = CGPointMake(newSize.width/imageRect.size.width,
                            newSize.height/imageRect.size.height);

// AffineTransformでサイズを変更し、切り抜く
CIImage *filteredImage = [sourceImage imageByApplyingTransform:CGAffineTransformMakeScale(scale.x,scale.y)];
filteredImage = [filteredImage imageByCroppingToRect:CGRectMake(0, 0, newSize.width,newSize.height)];

// UIImageに変換する
CIContext *ciContext = [CIContext contextWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
                                         forKey:kCIContextUseSoftwareRenderer]];

CGImageRef imageRef = [ciContext createCGImage:filteredImage fromRect:[filteredImage extent]];
UIImage *outputImage  = [UIImage imageWithCGImage:imageRef scale:1.0f orientation:UIImageOrientationUp];
CGImageRelease(imageRef);
60
59
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
60
59