UICollectionViewCell快照

问题描述 投票:0回答:1

[code]我需要为提供的索引路径拍摄一个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
}

[码]

uicollectionviewcell snapshot
1个回答
0
投票

为所选索引路径拍摄集合视图单元格的快照。试试这个快照

    var yourImage: UIImage?
    UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, false, UIScreen.main.scale)
    yourView.drawHierarchy(in: myView.bounds, afterScreenUpdates: true)
    yourImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return yourImage!
© www.soinside.com 2019 - 2024. All rights reserved.