【发布时间】: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