ios swift uicollectionviewcell快照错误



当我尝试在uicollectionView中拍摄某个uicollectionViewCell的快照时,这是视图的子视图,并通过我的应用程序在Facebook上共享。

致命错误:未包装可选值

时出乎意料地发现了无效

请单击此处以获取该错误的屏幕截图

这是我的功能

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)
    }
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)
    }

制作这样的函数。它将解决您的问题。当您称之为此功能时,它会为您创建单元格,以免获得零单元格

现在正在使用最小的更改。谢谢大家!

 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)
}

最新更新