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
fatal error: unexpectedly found nil while unwrapping an Optional value
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
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)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.