LoginSignup
10
10

More than 5 years have passed since last update.

CIFilterを使った簡単な画像加工

Last updated at Posted at 2012-05-11

UIImageにCIFilterをかけてその結果をUIImageで取り出す。
画像の四隅が暗くなるVignetteフィルターでテスト。

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

// CIFilterを作成し、ソース画像とエフェクトのパラメータをセットする
CIFilter *filter = [CIFilter filterWithName:@"CIVignette"]; 
[filter setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputIntensity"];
[filter setValue:[NSNumber numberWithFloat:2.0] forKey:@"inputRadius"];
[filter setValue:filteredImage forKey:@"inputImage"];
// 結果を取り出す
filteredImage = filter.outputImage;

/*
 // CIFilterはチェーンが可能。コメント外すとCIVignetteが二重にかかる
 [filter setValue:filteredImage forKey:@"inputImage"];
 filteredImage = filter.outputImage;
 */

// CIImageをUIImageに変換する
CIContext *ciContext = [CIContext contextWithOptions:nil];
CGImageRef imageRef = [ciContext createCGImage:filteredImage fromRect:[filteredImage extent]];
UIImage *outputImage  = [UIImage imageWithCGImage:imageRef scale:1.0f orientation:UIImageOrientationUp];
CGImageRelease(imageRef);

// 表示
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imageView setImage:outputImage];

[self.view addSubview:imageView];
10
10
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
10
10