[代码] 我需要为提供的indexpath拍摄collectionview单元格的快照,我创建了以下函数,并且始终将图像返回为nil
请告诉我这里出了什么问题
func screenshotForCellAtIndexPath(indexPath: IndexPath!, rect: CGRect!) -> UIImage?
{
let cellRect = rect
UIGraphicsBeginImageContextWithOptions(cellRect!.size, false, 0.0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.render(in: context)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
[代码]
答案 0 :(得分:0)
为选定的索引路径拍摄“收集”视图单元的快照。 尝试为此拍摄快照
var yourImage: UIImage?
UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, false, UIScreen.main.scale)
yourView.drawHierarchy(in: myView.bounds, afterScreenUpdates: true)
yourImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return yourImage!