Swift - 屏幕更新后 - UIView 到 UIImage 的转换

Swift - After Screen Updates - UIView to UIImage Conversion

我正在尝试创建一个 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)
}

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

View (0x7fe876e2d940, UIView) drawing with afterScreenUpdates:YES inside CoreAnimation commit is not supported

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

[Snapshotting] Drawing a view that has not been rendered at least once requires afterScreenUpdates: YES.

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

试试这个它的工作代码 (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()
        }
    }
}

输出: