【问题标题】:Swift - After Screen Updates - UIView to UIImage ConversionSwift - 屏幕更新后 - UIView 到 UIImage 的转换
【发布时间】:2019-02-27 14:20:23
【问题描述】:

我正在尝试创建一个UIImage,我将使用它来设置UICollectionViewCell 的背景。我发现这个函数可以将UIView 转换为UIImage

func imageWithView(view: UIView) -> UIImage? {
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.isOpaque, 0.0)
    view.drawHierarchy(in: view.bounds, afterScreenUpdates: true)
    let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return snapshotImage
}

在我的集合视图单元格函数中我这样做:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! VideoCell

    //creating view
    let colour1 = UIColor.yellow.cgColor
    let colour2 = UIColor.red.cgColor
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = contentView.bounds
    gradientLayer.colors = [colour1, colour2]
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
    let gradientView = UIView(frame: contentView.bounds)
    gradientView.layer.addSublayer(gradientLayer)

    //converting UIView to UIImage
    let ccimage = imageWithView(view: gradientView)
}

但是,我不断收到此错误:

使用 afterScreenUpdates:YES 查看 (0x7fe876e2d940, UIView) 绘图 不支持 CoreAnimation 内部提交

如果我创建 afterScreenUpdates: false ,则会收到此错误:

[快照] 绘制至少一次未渲染的视图 需要 afterScreenUpdates:是。

我真的很困惑如何处理这个错误。我是不是在错误的地方调用了该函数?

【问题讨论】:

  • 尝试在此之后添加layoutIfNeeded
  • 到我的 UIView?我添加了 gradientView.layoutIfNeeded() ,它仍然给出同样的错误
  • 试试cell
  • 我放了 cell.layoutIfNeeded() 还是一样的错误
  • @awezmm 你这样做是为了给集合视图单元格设置渐变背景吗?

标签: ios swift uiview uiimage core-animation


【解决方案1】:

试试这个它的工作代码(swift 4)

在 UICollectionView cellForItemAt 方法中。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! VideoCell

    //creating view
    let colour1 = UIColor.yellow.cgColor
    let colour2 = UIColor.red.cgColor
    let gradientLayer = CAGradientLayer()
    gradientLayer.frame = contentView.bounds
    gradientLayer.colors = [colour1, colour2]
    gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
    gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
    let gradientView = UIView(frame: contentView.bounds)
    gradientView.layer.addSublayer(gradientLayer)

    //converting UIView to UIImage
    if cell.backgroundColor == nil{
        cell.backgroundColor = UIColor(patternImage:gradientView.gradient_image!)
    }
    return cell
}

UIView 转换为UIImage 的扩展名

extension UIView {

    var gradient_image: UIImage? {

        if #available(iOS 10.0, *) {
            let renderer = UIGraphicsImageRenderer(bounds: bounds)
            return renderer.image { rendererContext in
                layer.render(in: rendererContext.cgContext)
            }
        } else {

            // If Swift version is lower than 4.2,
            UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.isOpaque, 0.0)
            defer { UIGraphicsEndImageContext() }
            guard let currentContext = UIGraphicsGetCurrentContext() else {
                return nil
            }
            self.layer.render(in: currentContext)
            return UIGraphicsGetImageFromCurrentImageContext()
        }
    }
}

输出:

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多