作为背景的被弄脏的图象在UIView

时间:2018-01-25 07:02:05

标签: ios swift

所以我有一个我在视图中渲染的图像。对于该uiview的背景颜色,我实际上想要UIImage。我的方式是使用currentImageView并应用此函数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的lazy 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
}()

知道我怎么会这样做吗? 我实施的当前方式会模糊原始图像,将模糊图像更改为主图像并将背景图像转换为不模糊的图像

1 个答案:

答案 0 :(得分:2)

使用以下扩展名:

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
}()