【问题标题】:CIFilter not working properly, returns null imageCIFilter 无法正常工作,返回空图像
【发布时间】:2011-12-28 04:51:21
【问题描述】:

大家好,我一直在使用一些过滤器。并非所有这些似乎都能正常工作,例如 CISepiTone 和 CIHueAdjust。 最近我尝试了 CIGLoom 过滤器,它返回一个空图像。

-(UIImage*)getGloom:(UIImage*)anImage{
    CGImageRef cgimage = anImage.CGImage;
    CIImage *cimage = [CIImage imageWithCGImage:cgimage];
    CIFilter *filter = [CIFilter filterWithName:@"CIGloom"];
    [filter setDefaults];
    [filter setValue: cimage forKey: @"inputImage"];
    [filter setValue: [NSNumber numberWithFloat: 25]
             forKey: @"inputRadius"];
    [filter setValue: [NSNumber numberWithFloat: 0.75]
             forKey: @"inputIntensity"];

    CIContext *context = [CIContext contextWithOptions:nil];
    CIImage *ciimage = [filter outputImage];
    CGImageRef cgimg = [context createCGImage:ciimage fromRect:[ciimage extent]];
    UIImage *uimage = [UIImage imageWithCGImage:cgimg scale:1.0f orientation:UIImageOrientationUp];

    CGImageRelease(cgimg);

    return uimage; }

我实际上是从今年的 techtalk 世界巡回演唱会中获得了这个代码,它适用于 CISepiaTone,但它对 cigloom 无效。 cicolorposterize、ciedges 和其他一些。有人知道为什么吗?或者如何绕过这个 NULL IMAGE?

【问题讨论】:

    标签: iphone ios ios5 core-graphics


    【解决方案1】:

    iOS 尚不支持 CIGloom 以及您遇到问题的其他工具。您可以使用此数组结果检查您有哪些可用的过滤器:

    NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    

    【讨论】:

      【解决方案2】:

      是的,有些过滤器似乎还不适用于 iOS。阅读文档时有同样有趣的体验。但是,您可以检查代码以查看适用于 iOS 的过滤器,如上述扬声器。一些过滤器,例如我在文档中没有找到的 CIVingette,我这样做也是为了在 5.1 上为每个可用的 iOS 过滤器获取值参数。

      NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
      for (CIFilter *filter in supportedFilters) {
          NSString *string = [NSString stringWithFormat:@"%@",[[CIFilter filterWithName:(NSString *)filter] inputKeys]];
          NSLog(@"%@ %@", filter, string);
      }
      

      回应:

      ... 2012-04-19 14:02:55.597 ImageFiltering[12190:707] CIVibrance ( 输入图像, 输入金额 ) 2012-04-19 14:02:55.599 ImageFiltering[12190:707] CIVignette ( 输入图像, 输入强度, 输入半径 ) 2012-04-19 14:02:55.601 ImageFiltering[12190:707] CIWhitePointAdjust ( 输入图像, 输入颜色 ) ...

      请注意,将来(或者有人已经知道您是否可以阅读有关它的更多信息)您可能想要阅读文档。这只是我扮演侦探来绕过它,因为我没有找到我前面提到的某些过滤器的任何文档。

      这是我从以前的信息中得到的 CIVingette 示例:

      - (void)displayVingetteFilterWithImage{ // 获取图像并将其设置为 CIImage NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"]; NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath]; CIImage *image = [CIImage imageWithContentsOfURL:fileNameAndPath]; // 创建上下文 CIContext *imageContext = [CIContext contextWithOptions:nil]; // 将过滤器设置为图像,在本例中为 CIVignette,知道它使用来自先前日志响应的 inputImage、inputIntensity 和 inputRadius。 CIFilter *vignette = [CIFilter filterWithName:@"CIVignette"]; [小插图集默认值]; [vignette setValue: image forKey: @"inputImage"]; [vignette setValue: [NSNumber numberWithFloat: 5.0] forKey:@"inputIntensity"]; [vignette setValue: [NSNumber numberWithFloat: 30.0] forKey:@"inputRadius"]; // 将 CIImage 附加到 CGImageRef 并附加为 UIImage CIImage *result = [vignette valueForKey:@"outputImage"]; CGImageRef cgImageRef = [imageContext createCGImage:result fromRect:[result extent]]; UIImage *targetImage = [UIImage imageWithCGImage:cgImageRef]; // 将 UIImage 附加到 self.view 中的 UIImageView 上,也定位它,只是为了好玩。 UIImageView *imageView = [[UIImageView alloc] initWithImage:targetImage]; [self.view addSubview:imageView]; [imageView setImage:targetImage]; imageView.frame = CGRectMake(0.0, 10.0, imageView.frame.size.width, imageView.frame.size.height); // 释放我们之前创建的 CGImageRef。 CGImageRelease(cgImageRef); }

      【讨论】:

      • 投票支持你,只是为了最终结束我对 CIVignette 的痛苦。为什么属性说最大强度为1,最大半径为2??!!!我应该尝试不同的数字。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 2013-04-17
      • 2015-07-25
      • 2019-01-11
      • 2012-07-04
      • 1970-01-01
      相关资源
      最近更新 更多