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?
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?
Not perfect but pretty good:
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)
}
}
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)
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)
}
How about adding a black low opacity overlay subview to your original view?
There are several possible solutions here:
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