简体   繁体   English

iOS Swift UICollectionViewCell快照错误

[英]iOS swift UICollectionviewcell snapshot error

When i tried to take snapshot a certain UICollectionViewCell in a UICollectionview which is a subview of View and share it on Facebook from my app..while Xcode throws 当我尝试对UICollectionview中的某个UICollectionViewCell进行快照时,该UICollectionview是View的子视图,并从我的应用程序在Facebook上共享它。

fatal error: unexpectedly found nil while unwrapping an Optional value 致命错误:解开Optional值时意外发现nil

please click here for screenshot of that error 请点击此处查看该错误的屏幕截图

Here is my function 这是我的功能

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)

    }

make the func like this.it will solve your problem.When you called this function it create the cell for you so its not getting nil cell 像这样制作函数。它将解决您的问题。当您调用此函数时,它会为您创建一个单元格,因此不会得到nil单元格

It is now working with minimal changes. 现在,它正在进行最小的更改。 Thanks everyone! 感谢大家!

 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