简体   繁体   中英

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. recently i tried CIGLoom filter and it returns a null image.

-(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. cicolorposterize, ciedges and some others. Anyone got any idea why? or how to get around this NUll IMAGE?

CIGloom, and others you've been having problems with, aren't supported yet on iOS. 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. 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. 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.

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:

- (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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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