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

时间:2018-09-24 05:25:16

标签: ios swift uiview uiimage core-animation

我正在尝试创建一个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查看(0x7fe876e2d940,UIView)绘图是   不支持在CoreAnimation内部提交

如果我输入afterScreenUpdates: false,则会出现此错误:

  

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

我对如何处理此错误感到非常困惑。我在错误的地方调用了函数吗?

1 个答案:

答案 0 :(得分:0)

尝试一下(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()
        }
    }
}

输出:

enter image description here