模糊的图像作为UIView的背景



所以我有一个在视图中渲染的图像。对于该 uiview 的背景颜色,我实际上想要一个UIImage。我的做法是采用当前的图像视图并将此功能应用于makeBlurEffect

  func makeBlurImage(targetImageView:UIImageView?)
    {
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = targetImageView!.bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] // for supporting device rotation
        targetImageView?.addSubview(blurEffectView)
    }

我通常设置背景图像并在创建UIView的惰性var方法中应用模糊滤镜,如下所示。

lazy var blurryBackGround : UIView = {
    let blurryBackGround = UIView()
    blurryBackGround.backgroundColor = UIColor.red
    // blurryBackGround.backgroundColor = UIColor(i)
    var blurryImage = currentImageView
    blurryImage.makeBlurImage(blurryImage?)
    return blurryBackGround
}()

我还包括了UIImageView创作:

//
lazy var currentEventImage : UIImageView = {
    let currentEvent = UIImageView()
    currentEvent.clipsToBounds = true
    currentEvent.translatesAutoresizingMaskIntoConstraints = false
    currentEvent.contentMode = .scaleAspectFill
    currentEvent.isUserInteractionEnabled = true
    currentEvent.layer.masksToBounds = true
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handlePromoVid))
    currentEvent.isUserInteractionEnabled = true
    currentEvent.addGestureRecognizer(tapGestureRecognizer)
    return currentEvent
}()

知道我将如何正确执行此操作吗?我目前实现的方式是模糊原始图像,将模糊图像更改为主图像,并将背景图像转换为未模糊的图像

使用以下扩展名:

extension UIImageView {
    func renderedImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, UIScreen.main.scale)
        self.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}

要捕获当前呈现的imageView UIImage

编辑

你试着不要捕捉它怎么样,只是把它作为背景:

lazy var blurryBackGround : UIView = {
    let blurryBackGround = currentImageView
    blurryBackGround.makeBlurImage(blurryBackGround)
    return blurryBackGround
}()

最新更新