首页 > 解决方案 > UIBlurEffect 不会模糊 UITableViewCell 中的整个图像

问题描述

我有以下代码来模糊图像。

    let blurEffect = UIBlurEffect(style: .light)
    let blurredEffectView = UIVisualEffectView(effect: blurEffect)
    blurredEffectView.frame = imageBlur.bounds
    //blurredEffectView.alpha = 0.8
    imageBlur.addSubview(blurredEffectView)
    
    imageBlur.sd_setImage(with: URL(string: item._galleryURL), placeholderImage: UIImage(named: ""))

但是,当我运行代码时,我在 UITableView 中的前两个元素中看到了这一点。

在此处输入图像描述

当我向下滚动然后再次备份时,它们将修复...

在此处输入图像描述

什么可能导致此错误?

标签: iosswiftuitableview

解决方案


当您在 tableView 单元格中创建模糊视图时,您的视图现在不是确切的大小。因此,在第一次创建屏幕时它看起来不太好,但是当您向下和向上滚动时它会回收并知道它的大小就可以了。

所以你可以通过在 layoutsubviews() 函数中给出大小来解决它

    override func layoutSubviews() {
    super.layoutSubviews()
    blurredEffectView.frame = imageBlur.bounds
}

推荐阅读