iOS 6上的CIGaussianBlur和CIAffineClamp

时间:2012-10-16 08:37:34

标签: iphone objective-c ios core-image

我试图在iOS 6上使用CoreImage模糊图像而没有明显的黑色边框。 Apple documentation声明使用CIAffineClamp过滤器可以实现此目的,但我无法从过滤器中获取输出图像。这是我尝试过的,但不幸的是,当我访问[clampFilter outputImage]时会创建一个空图像。如果我只执行模糊,则会生成图像,但是会出现黑色边框。

CIImage *inputImage = [[CIImage alloc] initWithCGImage:self.CGImage];

CIContext *context = [CIContext contextWithOptions:nil];

CGAffineTransform transform = CGAffineTransformIdentity;

CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
[clampFilter setValue:inputImage forKey:kCIInputImageKey];
[clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];

CIImage *outputImage = [clampFilter outputImage];

CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"
     keysAndValues:kCIInputImageKey, outputImage, @"inputRadius", [NSNumber numberWithFloat:radius], nil];

outputImage = [blurFilter outputImage];

CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *blurredImage = [UIImage imageWithCGImage:cgimg];
CGImageRelease(cgimg);

1 个答案:

答案 0 :(得分:15)

CIAffineClamp过滤器将您的范围设置为无限,然后会混淆您的上下文。尝试保存预钳位范围CGRect,然后将其提供给上下文初始化程序。