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];