UIImage+BlurAndDarken 0.0.1

UIImage+BlurAndDarken 0.0.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.


Downloads

Total2,623
Week0
Month0

Installs

Apps28
Test Targets3
Watch Apps1
powered by Segment

GitHub

Stars39
Watchers1
Forks4
Issues0
Contributors2
Pull Requests0

Code

Files2
LOCLines of Code 36


  • By
  • Bryan Clark

ios-darken-image-with-cifilter

I was frustrated trying to get a CIFilter that'd work to darken an image by 50% (the equivalent, in photoshop, to adding a 0.5-alpha black layer in front of a picture). An hour later, I figured it out. Enjoy!

Here's the code that does the magic:

-(instancetype)darkened:(CGFloat)alpha andBlurredImage:(CGFloat)radius blendModeFilterName:(NSString *)blendModeFilterName {

    CIImage *inputImage = [[CIImage alloc] initWithImage:self];

    CIContext *context = [CIContext contextWithOptions:nil];

    //First, create some darkness
    CIFilter* blackGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
    CIColor* black = [CIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:alpha];
    [blackGenerator setValue:black forKey:@"inputColor"];
    CIImage* blackImage = [blackGenerator valueForKey:@"outputImage"];

    //Second, apply that black
    CIFilter *compositeFilter = [CIFilter filterWithName:blendModeFilterName];
    [compositeFilter setValue:blackImage forKey:@"inputImage"];
    [compositeFilter setValue:inputImage forKey:@"inputBackgroundImage"];
    CIImage *darkenedImage = [compositeFilter outputImage];

    //Third, blur the image
    CIFilter *blurFilter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [blurFilter setDefaults];
    [blurFilter setValue:@(radius) forKey:@"inputRadius"];
    [blurFilter setValue:darkenedImage forKey:kCIInputImageKey];
    CIImage *blurredImage = [blurFilter outputImage];

    CGImageRef cgimg = [context createCGImage:blurredImage fromRect:inputImage.extent];
    UIImage *blurredAndDarkenedImage = [UIImage imageWithCGImage:cgimg];
    CGImageRelease(cgimg);

    return blurredAndDarkenedImage;
}

In a few lines of code, YOU TOO can witness this miracle transformation: iOS Screenshot