我可以使用本地图像创建 CIGausianBlur 过滤器。现在,我正在尝试使用相同的格式创建一个 CIKaleidoscope 过滤器和 CILightTunnel。我的万花筒过滤器代码是:
- (UIImage *)applyKaleidoscopeToImage:(UIImage *)image {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *ci_image = [CIImage imageWithCGImage:image.CGImage];
CIFilter *filter = [CIFilter filterWithName:@"CIKaleidoscope"];
[filter setValue:ci_image forKey:kCIInputImageKey];
[filter setValue:@55 forKey:kCIInputAngleKey];
CIVector *civ = [CIVector vectorWithX:50 Y:50];
[filter setValue:civ forKey:kCIInputCenterKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
return [UIImage imageWithCGImage:cgImage scale:image.scale orientation:image.imageOrientation];
}
对于这种方法,我得到一个 NSException 类型的未捕获异常,并警告尝试用 nil 值替换数组对象。
我的 CILightFilter 代码如下:
- (UIImage *)applyLightTunnelToImage:(UIImage *)image {
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *ci_image = [CIImage imageWithCGImage:image.CGImage];
CIFilter *filter = [CIFilter filterWithName:@"CILightTunnel"];
[filter setValue:ci_image forKey:kCIInputImageKey];
CIVector *civ = [CIVector vectorWithX:50 Y:50];
[filter setValue:civ forKey:kCIInputCenterKey];
[filter setValue:@5 forKey:kCIInputRadiusKey];
//[filter setValue:@45 forKey:kCIInputAngleKey];
CIImage *result = [filter valueForKey:kCIOutputImageKey];
CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
return [UIImage imageWithCGImage:cgImage scale:image.scale orientation:image.imageOrientation];
}
我在这里也得到相同的 nil 结果。提前感谢你的帮助!