2017-04-20 1 views
0

Quand j'ai essayé de prendre un instantané d'un certain UICollectionViewCell dans un UICollectionview qui est une sous-vue de vue et de le partager sur Facebook de mon app..while Xcode jetteerreur de capture instantanée iOS UICollectionviewcell rapide

fatal error: unexpectedly found nil while unwrapping an Optional value

please click here for screenshot of that error

Voici ma fonction

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

Déboguer et vérifier l'instance self.bsCell, est-ce nul? – Surjeet

+0

application s'écraser sur la ligne self.bsCell.layer.renderInContext ((UIGraphicsGetCurrentContext())!) –

+0

Où appelez-vous 'screenshotBSCell'? Le problème est que 'UIGraphicsGetCurrentContext()' renvoie un résultat nul, que vous essayez alors de forcer (ne faites pas cela :)) ce qui provoque un plantage. Donc ... il semble que votre appel à 'UIGraphicsBeginImageContext' ne fonctionne pas comme prévu. Comme @Surjeet le suggère ... vérifiez que votre 'bsCell' n'est pas nul, et vérifiez également la taille de' bsCell' pour vous assurer qu'il est valide. – pbodsk

Répondre

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

    } 

faire la fonc lik e cela.il va résoudre votre problème.Lorsque vous avez appelé cette fonction, il crée la cellule pour vous afin qu'il ne reçoive pas de cellule 0il

0

Il fonctionne maintenant avec un minimum de changements. Merci tout le monde!

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) 

}