的iOS迅速UICollectionviewcell快照错误

问题描述:

当我试图采取一个UICollectionview是视图的子视图快照一定UICollectionViewCell和分享在Facebook上从我app..while的Xcode抛出的iOS迅速UICollectionviewcell快照错误

fatal error: unexpectedly found nil while unwrapping an Optional value

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) 

    } 
+0

调试并检查self.bsCell实例,它是否为零? – Surjeet

+0

app crashing at the self.bsCell.layer.renderInContext(((UIGraphicsGetCurrentContext())!) –

+0

你在哪里调用'screenshotBSCell'?问题是'UIGraphicsGetCurrentContext()'返回nil,然后你尝试强制解包(不要那么做),导致崩溃。所以......看起来你对'UIGraphicsBeginImageContext'的调用不能按预期工作。正如@Surjeet建议的那样...验证你的'bsCell'不是零,并检查'bsCell'的大小以确保它是有效的。 – pbodsk

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 LIK e this.it将解决你的问题。当你调用这个函数时,它会为你创建单元格,所以它不会得到零单元格。

它现在正在进行最小的更改。感谢大家!

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) 

}