user2417339
user2417339

Reputation:

Objective C: CIFilter returning nil image

I am writing a function that will apply a filter to an image and return the new image. I wrote the following code:

+ (UIImage*)applyFilter:(UIImage*) photo {
    CIImage *image = [[CIImage alloc] initWithCGImage:photo.CGImage];
    CIFilter *filter = [CIFilter filterWithName:@"CIphotoEffectTransfer"
                                  keysAndValues: kCIInputImageKey, image,
                        @"inputIntensity", @0.8, nil];
    CIImage *outputImage = [filter outputImage];

    UIImage* newPhoto = [self imageFromCIImage:outputImage];
    return newPhoto;
}

The problem I am running into is that the function is returning a nil photo instead of one with a filter applied. Interestingly, if I change the filter name to @"CIVignetteEffect" it will work. I don't understand why one filter works but the other will not. I found both of the filters from the following link: https://developer.apple.com/library/tvos/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIPhotoEffectTransfer

Upvotes: 0

Views: 961

Answers (2)

Jaywant Khedkar
Jaywant Khedkar

Reputation: 6141

Try This ,

We have implemented CIFilter effects ,

       //CIVignette Effect 

        CIContext *imageContext = [CIContext contextWithOptions:nil];
        CIImage *image = [[CIImage alloc] initWithImage:inputimage];

        CIFilter *vignette = [CIFilter filterWithName:@"CIVignette"];
        [vignette setDefaults];
        [vignette setValue: image forKey: @"inputImage"];
        [vignette setValue: [NSNumber numberWithFloat: 1.0] forKey: @"inputIntensity"];
        [vignette setValue: [NSNumber numberWithFloat: 10.00 ] forKey: @"inputRadius"];
         CIImage *result = [vignette valueForKey: @"outputImage"];
        CGImageRef cgImageRef = [imageContext createCGImage:result fromRect:[result extent]];
        UIImage *targetImage = [UIImage imageWithCGImage:cgImageRef];

For detail implementation of multiple effect you can refer this GitHub project file ImageFilter

Hope so this answer will help for some one .

Upvotes: 0

Dean
Dean

Reputation: 939

I believe the correct name of the filter is CIPhotoEffectTransfer, not CIphotoeffectTransfer.

Try this code...I have used it for photo filtering in the past so I know it works:

+ (UIImage*)applyFilter:(UIImage*) photo {
UIImageOrientation orientation = photo.imageOrientation;
CIImage* image = [CIImage imageWithCGImage:photo.CGImage];
CIContext *context = [CIContext contextWithOptions:nil];
CIFilter *filter = [CIFilter filterWithName:@"CIPhotoEffectTransfer"];
[filter setValue:image forKey:kCIInputImageKey];
CIImage *outputImage = [filter outputImage];
CGImageRef cgimg = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newPhoto = [UIImage imageWithCGImage:cgimg scale:1.0 orientation:orientation];
CGImageRelease(cgimg);
context = nil;
return newPhoto;

}

Upvotes: 2

Related Questions