1

I've searched a lot, but i can't find how you can darken an UIImageView if you press a button...

I've tried it with filters etc, but it did not work.

So how can I do this?

Popeye
  • 11,839
  • 9
  • 58
  • 91

6 Answers6

2

Not perfect but pretty good:

enter image description here

public extension UIImage {

    private enum BlendMode {
        case multiply // This results in colors that are at least as dark as either of the two contributing sample colors
        case screen // This results in colors that are at least as light as either of the two contributing sample colors

    }

    // A level of zero yeilds the original image, a level of 1 results in black
    func darken(level: CGFloat = 0.5) -> UIImage? {
        return blend(mode: .multiply, level: level)
    }

    // A level of zero yeilds the original image, a level of 1 results in white
    func lighten(level: CGFloat = 0.5) -> UIImage? {
        return blend(mode: .screen, level: level)
    }

    private func blend(mode: BlendMode, level: CGFloat) -> UIImage? {
        let context = CIContext(options: nil)

        var level = level
        if level < 0 { level = 0 }
        else if level > 1 { level = 1 }

        let filterName: String
        switch mode {
        case .multiply: // As the level increases we get less white
            level = abs(level - 1.0)
            filterName = "CIMultiplyBlendMode"
        case .screen: // As the level increases we get more white
            filterName = "CIScreenBlendMode"
        }

        let blender = CIFilter(name: filterName)!
        let backgroundColor = CIColor(color: UIColor(white: level, alpha: 1))

        guard let inputImage = CIImage(image: self) else { return nil }
        blender.setValue(inputImage, forKey: kCIInputImageKey)

        guard let backgroundImageGenerator = CIFilter(name: "CIConstantColorGenerator") else { return nil }
        backgroundImageGenerator.setValue(backgroundColor, forKey: kCIInputColorKey)
        guard let backgroundImage = backgroundImageGenerator.outputImage?.cropped(to: CGRect(origin: CGPoint.zero, size: self.size)) else { return nil }
        blender.setValue(backgroundImage, forKey: kCIInputBackgroundImageKey)

        guard let blendedImage = blender.outputImage else { return nil }

        guard let cgImage = context.createCGImage(blendedImage, from: blendedImage.extent) else { return nil }
        return UIImage(cgImage: cgImage)
    }
}
Verticon
  • 2,419
  • 16
  • 34
1

You can add CAGradientLayer in imageView.Add below code into button press event.

let  gradientLayer   = CAGradientLayer()
gradientLayer.frame  =  self.imageView.bounds
gradientLayer.colors = [UIColor.clearColor().CGColor,UIColor.blackColor().CGColor] 
self.imageView.layer.addSublayer(gradientLayer)
Donal
  • 6,082
  • 2
  • 19
  • 30
1

Updated answer for Swift 5:

func addGradientOverlay() {
  let  gradientLayer   = CAGradientLayer()
  gradientLayer.frame  =  self.imageView.bounds
  gradientLayer.colors = [UIColor.clear.cgColor,UIColor.black.cgColor] 
  self.imageView.layer.addSublayer(gradientLayer)
}
Tripti Kumar
  • 1,559
  • 14
  • 28
0

To darken an UIImage you should try GPUImage, this framework has several filters like GPUImageBrightnessFilter, GPUImageContrastFilter and GPUImageSaturationFilter which may help to do what you need.

You can also find few examples on how to use this framework here.

ararog
  • 1,092
  • 10
  • 18
0

How about adding a black low opacity overlay subview to your original view?

There are several possible solutions here:

adding black overlay with 0.3 opacity over UIImageView

Community
  • 1
  • 1
sunshineDev
  • 323
  • 3
  • 10
-1

For a really easy but dumb approach I would suggest you create a new UIView with a black background colour, set its alpha to 0.5 or less, set its frame to your UIImageView.frame and put it on top of it:

UIView *tmpView = [[UIView alloc] initWithFrame:myImageView.frame.size.height];
tmpView.backgroundColor = [UIColor blackColor];
tmpView.alpha = 0.5
//add it on button press
[myImageView addSubview:tmpView];

Although I would not recommend to do this, it might be easier to grasp than all those GPUImage Filters

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120