简体   繁体   English

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

[英]CIFilter not working properly, returns null image

Hey all i've been using some filters. 嘿所有我一直在使用一些过滤器。 Not all of them seem to work normally like say for example CISepiaTone and CIHueAdjust. 并非所有这些似乎都能正常工作,例如CISepiaTone和CIHueAdjust。 recently i tried CIGLoom filter and it returns a null image. 最近我尝试了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; }

i actually got this code from the techtalk world tour this year and it works for CISepiaTone but it just fails for cigloom. 我实际上从今年的techtalk世界巡演中得到了这个代码,它适用于CISepiaTone,但它只是没有用于cigloom。 cicolorposterize, ciedges and some others. cicolorposterize,ciedges和其他一些。 Anyone got any idea why? 任何人都知道为什么? or how to get around this NUll IMAGE? 或者如何绕过这个NUll IMAGE?

CIGloom, and others you've been having problems with, aren't supported yet on iOS. 在iOS上,目前还不支持CIGloom以及其他您遇到过问题的人。 You can check which filters you have available using this array result: 您可以使用此数组结果检查可用的过滤器:

NSArray *supportedFilters = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];

Yes, it seems some filters are not available for iOS yet. 是的,似乎有些过滤器尚不适用于iOS。 Had the same fun experience when reading the documentation. 阅读文档时有同样有趣的体验。 You can however check with code to see which filter that is availible for iOS, as above speaker. 但是,您可以使用代码检查以查看适用于iOS的过滤器,如上面的扬声器。 Some filter for example CIVingette that I didn't find in the documentation, I did this to also get value-parameters for each available filter for iOS on 5.1. 有些过滤器例如CIVingette,我在文档中没有找到,我这样做也为每个可用于iOS的iOS过滤器获取值参数。

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

The response: 响应:

...
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
)
...

Note that in the future (or someone already knows were you can read more about it) you probably want to read the documentation. 请注意,将来(或者有人已经知道您可以阅读更多相关信息),您可能希望阅读文档。 This is just me playing detective to get around it, because I didn't find any documentation on some filters as I mention earlier. 这只是我玩侦探来绕过它,因为我没有找到任何关于某些过滤器的文档,正如我之前提到的那样。

Here is an example I got out with CIVingette from previous info: 以下是我从以前的信息中获取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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM