skip to Main Content

I am using the VariableBlur library in Swift to create a blur over my collection view under the status bar area as such:

I am trying to figure out how I can make the blur darker?

The library uses a CIFilter but I am not sure what to change to make it darker. I have already tried changing the UIBlurEffect(style: .regular) to use .dark but that didn’t make any difference.

And the color0 on ciGradientFilter is already set to black. In fact, changing it to red or any other color doesn’t make any difference.

2

Answers


  1. A simple way is to just put your own black gradient below the VariableBlurView.

    Image("some image")
        .overlay(alignment: .top) {
            ZStack {
                LinearGradient(colors: [.black, .clear], startPoint: .top, endPoint: .bottom)
                VariableBlurView(maxBlurRadius: 20, direction: .blurredTopClearBottom)
            }
            .frame(height: 400)
        }
    

    Side-by-side comparison:

    enter image description here enter image description here

    Login or Signup to reply.
  2. //import CoreImage
    
    func applyVariableBlurAndDarken(to image: UIImage) -> UIImage? {
        guard let ciImage = CIImage(image: image) else { return nil }
         // Apply VariableBlur Filter
        let variableBlurFilter = CIFilter(name: "CIMaskedVariableBlur")!
        variableBlurFilter.setValue(ciImage, forKey: kCIInputImageKey)
        variableBlurFilter.setValue(10.0, forKey: "inputRadius")  // Adjust the blur radius as needed
        
        guard let blurredImage = variableBlurFilter.outputImage else { return nil }
        
        // Apply Color Controls to darken the image
        let colorControlsFilter = CIFilter(name: "CIColorControls")!
        colorControlsFilter.setValue(blurredImage, forKey: kCIInputImageKey)
        colorControlsFilter.setValue(-0.5, forKey: kCIInputBrightnessKey) // Reduce brightness to darken
        colorControlsFilter.setValue(1.2, forKey: kCIInputContrastKey)   // Optional: increase contrast
        
        guard let darkenedImage = colorControlsFilter.outputImage else { return nil }
        
        // Render the final image
        let context = CIContext()
        guard let cgImage = context.createCGImage(darkenedImage, from: darkenedImage.extent) else { return nil }
        
        return UIImage(cgImage: cgImage)
    }
    
    // Usage
    if let image = UIImage(named: "your_image_name") {
        let processedImage = applyVariableBlurAndDarken(to: image)
        // Now you can use the processedImage
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search