簡體   English   中英

iOS Swift UICollectionViewCell快照錯誤

[英]iOS swift UICollectionviewcell snapshot error

當我嘗試對UICollectionview中的某個UICollectionViewCell進行快照時,該UICollectionview是View的子視圖,並從我的應用程序在Facebook上共享它。

致命錯誤:解開Optional值時意外發現nil

請點擊此處查看該錯誤的屏幕截圖

這是我的功能

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)

    }

像這樣制作函數。它將解決您的問題。當您調用此函數時,它會為您創建一個單元格,因此不會得到nil單元格

現在,它正在進行最小的更改。 感謝大家!

 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