简体   繁体   English

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

[英]Swift - After Screen Updates - UIView to UIImage Conversion

I am trying to create an UIImage that I will use to set the background for a UICollectionViewCell . 我正在尝试创建一个UIImage ,该图像将用于设置UICollectionViewCell的背景。 I found this function to convert a UIView to UIImage : 我发现此功能可以将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
}

In my collection view cell function I do: 在集合视图单元格函数中,我执行以下操作:

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

However, I keep getting this error: 但是,我不断收到此错误:

View (0x7fe876e2d940, UIView) drawing with afterScreenUpdates:YES inside CoreAnimation commit is not supported 不支持在CoreAnimation提交内使用afterScreenUpdates:YES查看(0x7fe876e2d940,UIView)绘图

If I make afterScreenUpdates: false , then I get this error: 如果我执行afterScreenUpdates: false ,那么我会收到此错误:

[Snapshotting] Drawing a view that has not been rendered at least once requires afterScreenUpdates: YES. [快照]绘制至少一次未渲染的视图需要afterScreenUpdates:是。

I'am really confused on how to approach this error. 我真的很困惑如何解决这个错误。 Am I calling the function in the wrong place? 我在错误的地方调用了函数吗?

Try this one it's working code for (swift 4) 尝试一下这是它的工作代码(速记4)

In UICollectionView cellForItemAt method. 在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
}

Extension fro convert UIView to UIImage 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()
        }
    }
}

Output : 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM