iOS swift UICollectionviewcell 快照错误

Posted

技术标签:

【中文标题】iOS swift UICollectionviewcell 快照错误【英文标题】:iOS swift UICollectionviewcell snapshot error 【发布时间】:2017-04-20 07:25:39 【问题描述】:

当我尝试在 UICollectionview 中拍摄某个 UICollectionViewCell 的快照时,该 UICollectionViewCell 是 View 的子视图,并从我的应用程序在 Facebook 上分享它......而 Xcode 抛出

致命错误:在展开可选值时意外发现 nil

please click here for screenshot of that error

这是我的功能

func screenshotBSCell() 
        //Create the UIImage

        UIGraphicsBeginImageContext(CGSizeMake(bsCell.bounds.size.width, bsCell.bounds.size.height))
        ***self.bsCell.layer.renderInContext(UIGraphicsGetCurrentContext()!)***
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        //Save it to the camera roll
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

        //share the image to Social Media
        let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        composeSheet.setInitialText("Hello, Facebook!")
        composeSheet.addImage(image)

        presentViewController(composeSheet, animated: true, completion: nil)

    

【问题讨论】:

调试并检查 self.bsCell 实例,是 nil 吗? 应用程序在 self.bsCell.layer.renderInContext((UIGraphicsGetCurrentContext())!) 行崩溃 你在哪里打电话给screenshotBSCell?问题是UIGraphicsGetCurrentContext() 返回 nil,然后您尝试强制解包(不要这样做:))导致崩溃。所以...看来您对UIGraphicsBeginImageContext 的呼叫没有按预期工作。正如@Surjeet 建议的那样...验证您的bsCell 不为零,并检查bsCell 的大小以确保它有效。 我在 cellForItemAtIndexPath 内调用 cell.shareBtn.addTarget(self, action: #selector(screenshotBSCell), forControlEvents: .TouchUpInside) 【参考方案1】:
func screenshotBSCell( sender:AnyObject) 
        //Create the UIImage
        let indexpath = NSIndexPath.init(row: sender.tag, section: 0)

        self.bsCell = colletctionview!.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath ) as! bsCell
        UIGraphicsBeginImageContext(CGSizeMake(cell.bounds.size.width, cell.bounds.size.height))
        ***self.bsCell.layer.renderInContext(UIGraphicsGetCurrentContext()!)***
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        //Save it to the camera roll
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

        //share the image to Social Media
        let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        composeSheet.setInitialText("Hello, Facebook!")
        composeSheet.addImage(image)

        presentViewController(composeSheet, animated: true, completion: nil)

    

像这样制作函数。它将解决您的问题。当您调用此函数时,它会为您创建单元格,因此它不会得到零单元格

【讨论】:

【参考方案2】:

它现在正在以最小的变化工作。谢谢大家!

 func screenshotPulseCell(sender: UIButton) 

    //Create the UIImage
    UIGraphicsBeginImageContext(plCell.frame.size)
    self.plCell.contentView.layer.renderInContext((UIGraphicsGetCurrentContext())!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //Save it to the camera roll
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

    //share the image to Social Media
    let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
    composeSheet.setInitialText("My Pulse!")
    composeSheet.addImage(image)

    presentViewController(composeSheet, animated: true, completion: nil)


【讨论】:

以上是关于iOS swift UICollectionviewcell 快照错误的主要内容,如果未能解决你的问题,请参考以下文章

Swift5 踩过的坑和奇怪的API笔记

Swift5 踩过的坑和奇怪的API笔记

iOS 7 - UICollectionElementKindSectionHeader 使应用程序崩溃('UICollectionView 数据源未设置')

iOS开发进阶 - 自定义UICollectionViewLayout实现瀑布流布局

Storyboards + UIcollectionView:UI 在 iOS 模拟器和设备上的显示方式不同

iOS集合视图单元格高亮和选中的区别