首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionViewCell快照

UICollectionViewCell快照
EN

Stack Overflow用户
提问于 2019-04-05 03:02:58
回答 1查看 129关注 0票数 0

代码我需要为提供的indexpath拍摄集合视图单元格的快照,我已经创建了下面的函数,它总是将image返回为nil

请告诉我这里哪里做错了,

代码语言:javascript
运行
复制
    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
}

代码

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-05 09:48:03

为所选索引路径拍摄集合视图单元格的快照。尝试此操作以获取以下内容的快照

代码语言:javascript
运行
复制
    var yourImage: UIImage?
    UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, false, UIScreen.main.scale)
    yourView.drawHierarchy(in: myView.bounds, afterScreenUpdates: true)
    yourImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return yourImage!
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55523243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档