繁体   English   中英

iOS Swift UICollectionViewCell快照错误

[英]iOS swift UICollectionviewcell snapshot error

当我尝试对UICollectionview中的某个UICollectionViewCell进行快照时,该UICollectionview是View的子视图,并从我的应用程序在Facebook上共享它。

致命错误:解开Optional值时意外发现nil

请点击此处查看该错误的屏幕截图

这是我的功能

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)

    }

像这样制作函数。它将解决您的问题。当您调用此函数时,它会为您创建一个单元格,因此不会得到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)

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM