我搜索了很多,但是如果你按一下按钮,我就找不到如何使UIImageView变暗......
我已尝试使用过滤器等,但它无效。
那我怎么能这样做呢?
答案 0 :(得分:1)
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)
}
答案 1 :(得分:0)
要使UIImage变暗,你应该尝试GPUImage,这个框架有几个过滤器,如GPUImageBrightnessFilter,GPUImageContrastFilter和GPUImageSaturationFilter,可以帮助你做你需要的。
您还可以找到有关如何使用此框架here的几个示例。
答案 2 :(得分:0)
答案 3 :(得分:0)
您可以将以下代码中的imageView.Add中的CAGradientLayer添加到按钮事件中。
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.imageView.bounds
gradientLayer.colors = [UIColor.clearColor().CGColor,UIColor.blackColor().CGColor]
self.imageView.layer.addSublayer(gradientLayer)
答案 4 :(得分:0)
不完美,但还不错:
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)
}
}
答案 5 :(得分:-1)
对于一个非常容易但又愚蠢的方法,我建议你创建一个黑色背景颜色的新UIView,将其alpha设置为0.5或更小,将其框架设置为UIImageView.frame并将其置于其上:
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];
虽然我不建议这样做,但它可能比所有那些GPUImage过滤器更容易掌握