iOS: create an darker version of UIImage and leave transparent pixels unchanged?

前端 未结 4 555
小蘑菇
小蘑菇 2021-02-05 16:31

I found

Create new UIImage by adding shadow to existing UIImage

and

UIImage, is there an easy way to make it darker or all black

But the selected

4条回答
  •  感动是毒
    2021-02-05 17:23

    Here's a quick Swift version, using a CIExposureAdjust CIFilter :)

      // Get the original image and set up the CIExposureAdjust filter
      guard let originalImage = UIImage(named: "myImage"),
        let inputImage = CIImage(image: originalImage),
        let filter = CIFilter(name: "CIExposureAdjust") else { return }
    
      // The inputEV value on the CIFilter adjusts exposure (negative values darken, positive values brighten)
      filter.setValue(inputImage, forKey: "inputImage")
      filter.setValue(-2.0, forKey: "inputEV")
    
      // Break early if the filter was not a success (.outputImage is optional in Swift)
      guard let filteredImage = filter.outputImage else { return }
    
      let context = CIContext(options: nil)
      let outputImage = UIImage(CGImage: context.createCGImage(filteredImage, fromRect: filteredImage.extent))
    
      myImageView.image = outputImage // use the filtered UIImage as required.
    

提交回复
热议问题