简体   繁体   中英

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 . I found this function to convert a UIView to 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

If I make afterScreenUpdates: false , then I get this error:

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

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)

In UICollectionView cellForItemAt method.

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

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 :

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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