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

时间:2018-06-02 10:17:07

标签: swift xcode facebook uicollectionview share

如果我想将一个img从collectionView分享到Facebook,我就崩溃了。我通过CocoaPods使用Facebook SDK for swift。 如果我理解正确,那么是我的图片NIL中的URL。但另一方面,如果我点击“myImage”,img就会被加载。嗯有什么不对?

enter image description here

enter image description here

enter image description here

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

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

这是答案;-) DAMN ...自我而不是vc。 任何人都可以向我解释一下吗?有什么区别?

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