CIFilter无法正常工作,返回null图像

时间:2011-12-28 04:51:22

标签: iphone ios ios5 core-graphics

嘿,我一直在使用一些过滤器。并非所有这些似乎都能正常工作,例如CISepiaTone和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?

2 个答案:

答案 0 :(得分:5)

CIGloom和其他您遇到过问题的人在iOS上尚不支持。您可以使用此数组结果检查可用的过滤器:

NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];

答案 1 :(得分:4)

是的,似乎某些过滤器尚不适用于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 (
    inputImage,
    inputAmount
)
2012-04-19 14:02:55.599 ImageFiltering[12190:707] CIVignette (
    inputImage,
    inputIntensity,
    inputRadius
)
2012-04-19 14:02:55.601 ImageFiltering[12190:707] CIWhitePointAdjust (
    inputImage,
    inputColor
)
...

请注意,将来(或者有人已经知道您可以阅读更多相关信息),您可能需要阅读文档。这只是我玩侦探来绕过它,因为我没有找到任何关于某些过滤器的文档,正如我之前提到的那样。

以下是我从之前的信息中获得CIVingette的一个例子:

- (void)displayVingetteFilterWithImage{
    // Get image and set it as CIImage
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"];
    NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
    CIImage *image = [CIImage imageWithContentsOfURL:fileNameAndPath];

    // Create context 
    CIContext *imageContext = [CIContext contextWithOptions:nil];

    // Set filter to image, in this case CIVignette, knowing it uses inputImage, inputIntensity and inputRadius from previous log-response.
    CIFilter *vignette = [CIFilter filterWithName:@"CIVignette"];
    [vignette setDefaults];
    [vignette setValue: image forKey: @"inputImage"];
    [vignette setValue: [NSNumber numberWithFloat: 5.0] forKey: @"inputIntensity"];
    [vignette setValue: [NSNumber numberWithFloat: 30.0] forKey: @"inputRadius"];

    // Attach the CIImage to CGImageRef and attach it as UIImage
    CIImage *result = [vignette valueForKey: @"outputImage"];
    CGImageRef cgImageRef = [imageContext createCGImage:result fromRect:[result extent]];
    UIImage *targetImage = [UIImage imageWithCGImage:cgImageRef];

    // Attach UIImage to UIImageView in self.view, also position it, just for fun.
    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);

    //  Release CGImageRef we created earlier.
    CGImageRelease(cgImageRef);
}