简体   繁体   中英

Add CIFilter overlay over UIImage

I have the following code that puts a blur over my UIImage. After the blur is put over the UIImage, I would also like a black screen at opacity 75% put over the UIImage, and then combine these two effects (blur and black screen) into one UIImage.

Thank you in advance!

CIFilter *gaussianBlurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
                        [gaussianBlurFilter setDefaults];
                        [gaussianBlurFilter setValue:[CIImage imageWithCGImage:[bgPicture CGImage]] forKey:kCIInputImageKey];
                        [gaussianBlurFilter setValue:@0.7 forKey:kCIInputRadiusKey];
                        CIImage *outputImage = [gaussianBlurFilter outputImage];
                        CIContext *context  = [CIContext contextWithOptions:nil];
                        CGRect rect = [outputImage extent];
                        rect.origin.x   += (rect.size.width - bgPicture.size.width ) / 2;
                        rect.origin.y   += (rect.size.height - bgPicture.size.height) / 2;
                        rect.size   = bgPicture.size;

                        CGImageRef cgimg    = [context createCGImage:outputImage fromRect:rect];
                        UIImage *image  = [UIImage imageWithCGImage:cgimg];
                        CGImageRelease(cgimg);
                        _profilePhotos[username] = image;

Tried this (don't know for which value I am plugging in UIColor):

CIImage* filterInputImage = [CIImage imageWithCGImage:image.CGImage];

                        CIFilter* filter = [CIFilter filterWithName:@"CIConstantColorGenerator"];
                        [filter setValue:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:.75] forKey:kCIInputColorKey];
                        [filter setValue:filterInputImage forKey:kCIInputImageKey];

                        CIImage* filterOutputImage = filter.outputImage;

                        CIContext* ctx = [CIContext contextWithOptions:nil];
                        CGImageRef createdImage = [ctx createCGImage:filterOutputImage fromRect:filterOutputImage.extent];

                        UIImage* outputImages = [UIImage imageWithCGImage:createdImage];

                        _profilePhotos[username] = outputImages;

Apple released a category on UIImage that does just that, and it does it with one line of code:

UIImage *newImage = [image applyBlurWithRadius:0.7 tintColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:.75] saturationDeltaFactor:0.0 maskImage:nil];

There's even a helper method that adds a dark tint:

- (UIImage *)applyDarkEffect;

and one that takes a tint color and puts it at 60%:

- (UIImage *)applyTintEffectWithColor:(UIColor *)tintColor;

It's on github here: https://github.com/codefellows/sea-b4-ios/tree/master/UIImageEffects

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