正如我注意到的,当将 CIGaussianBlur 应用于图像时,图像的角变得模糊,因此它看起来比原始图像小。所以我发现我需要正确裁剪它以避免图像的透明边缘。但是如何根据模糊量计算我需要裁剪多少?
例子:
原图:
具有 50 个 CIGaussianBlur 输入半径的图像(蓝色是所有内容的背景):
最佳答案
以下面的代码为例...
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *inputImage = [[CIImage alloc] initWithImage:image];
CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
[filter setValue:inputImage forKey:kCIInputImageKey];
[filter setValue:[NSNumber numberWithFloat:5.0f] forKey:@"inputRadius"];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
这会生成您在上面提供的图像。但是,如果我改为使用原始图像 rect 从上下文中创建 CGImage,则生成的图像就是所需的大小。
CGImageRef cgImage = [context createCGImage:result fromRect:[inputImage extent]];
关于objective-c - 正确裁剪 CIGaussianBlur,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12839729/