将图像从 collectionView 共享到 Facebook - 可选值崩溃

Share Image from collectionView to Facebook - Crash of optional value

如果我想像这样将 collectionView 中的 img 分享到 Facebook,我会崩溃。我通过 CocoaPods 使用 Facebook SDK for swift。 如果我没理解错的话,那么我的图像中的 URL 就是 NIL。但另一方面,如果我单击 "myImage",则会加载 img。嗯,怎么了?

import FacebookLogin
import FacebookShare



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.layer.borderColor = UIColor.purple.cgColor
        cell?.layer.borderWidth = 2

        let imageCell = collectionView.cellForItem(at: indexPath) as! imageCellCollectionViewCell
        //        let imageCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! imageCellCollectionViewCell
        guard let myImage = imageCell.imageToShare.image else {
            print("leer")
            return
        }

        let photo = Photo(image: myImage, userGenerated: true)
        let content = PhotoShareContent(photos: [photo])
        do {
            try ShareDialog.show(from: myController, content: content)
        } catch {
            print(error)
        }
    }

您需要检查您的 myController 对象。也许是零:

if let vc = myController {
        do {
            try ShareDialog.show(from: vc, content: content)
        } catch {
            print(error)
        }
}

如果在 MyViewController 中实现了集合视图委托方法,那么您可以直接使用 Self 像这样:

do {
   try ShareDialog.show(from: Self, content: content)
 } catch {
   print(error)
 }

这就是答案 ;-) 该死的...自己而不是 vc。 谁能给我解释一下?有什么区别?

do {
            try ShareDialog.show(from: self, content: content)
        } catch {
            print(error)
        }