2017-04-20 9 views
0

은 엑스 코드아이폰 OS 신속 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.csCell.layer.renderInContext ((UIGraphicsGetCurrentContext()) 줄에서 응용 프로그램이 충돌 함) –

+0

여기서'screenshotBSCell'을 (를) 호출합니까? 문제는'UIGraphicsGetCurrentContext()'가 nil을 반환한다는 것입니다. 그런 다음 언 랩핑을 강요하려고 시도합니다. 그래서 ...'UIGraphicsBeginImageContext' 호출이 예상대로 작동하지 않는 것 같습니다. @Surjeet이 제안하는대로 ... bsCell이 nil이 아닌지 확인하고, bsCell의 크기를 또한 검사하여 유효한지 확인하십시오. – pbodsk

답변

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) 

    } 

FUNC의 사장님을 할 것입니다 이 this.it는 당신의 문제를 해결할 것입니다. 당신이이 함수를 호출했을 때 그것은 당신을 위해 셀을 생성합니다. 그래서 그것은 무효 셀을 얻지 못합니다.

0

이제는 최소한의 변경만으로 작동합니다. 모두에게 감사드립니다!

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) 

}