我希望能够以Boomerang的方式向Facebook发布视频

时间:2018-06-05 19:14:21

标签: swift url video sharing fbsdk

Facebook SDK文档显示您可以使用imagePickerController的网址将应用中的视频分享到Facebook:https://developers.facebook.com/docs/sharing/ios

但是,我希望能够从我的应用程序分享视频到Facebook,而无需进入图像选择器。通过首先将视频保存到用户的相机胶卷,然后显示用于共享视频的对话框,Boomerang就是这样做的。它看起来是保存视频的先决条件,并且能够将照片分享到Facebook,这与照片不同。

我可以将视频保存到我的照片库中,但是我无法让我的应用切换到Facebook来预览共享对话框。两个" TIC读状态"消息显示在我的控制台中,但没有错误。

为了更清楚我正在做什么,我将视频保存到我的库中,完成后,我按照创建日期将相机胶卷的媒体按降序排序,然后获取第一个对象。然后我将该资产转换为应该能够被FBSDKShareVideo使用的URL。

我的网址类似于" file:///var/mobile/Media/DCIM/129APPLE/IMG_9515.MP4"

CustomPhotoAlbum.sharedInstance.save(URL: self.fileURL, completion: {

    DispatchQueue.main.async(execute: {
        print(self.fileURL)
        self.enableButtons(enabled:true)
        self.activityTextStatus?.remove()
        self.timedLabel.setLabel("Video saved.", view:self.view)
    })

    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: self.fileURL)
    }) { saved, error in
        if saved {
            let fetchOptions = PHFetchOptions()
            fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

            let fetchResult = PHAsset.fetchAssets(with: .video, options: fetchOptions).firstObject

            PHImageManager().requestAVAsset(forVideo: fetchResult!, options: nil, resultHandler: { (avurlAsset, audioMix, dict) in
                let newObj = avurlAsset as! AVURLAsset

                let videoURL:URL = newObj.url

                print(videoURL)

                let photo = self.uiImages[0]

                let sharePhoto = FBSDKSharePhoto()
                sharePhoto.image = photo

                let filePath = self.fileURL

                // get size of video in bytes
                do {
                    var fileSize : UInt64
                    let attr = try FileManager.default.attributesOfItem(atPath: (filePath?.path)!)
                    fileSize = attr[FileAttributeKey.size] as! UInt64

                    print(fileSize)

                } catch {
                    print("Error: \(error)")
                }

                let shareVideo : FBSDKShareVideo = FBSDKShareVideo(videoURL: videoURL, previewPhoto: sharePhoto)

                let content : FBSDKShareVideoContent = FBSDKShareVideoContent()
                content.video = shareVideo

                let shareDialog: FBSDKShareDialog = FBSDKShareDialog()

                shareDialog.shareContent = content

                shareDialog.mode = .native
                shareDialog.show()

            })

        }
    }

})

0 个答案:

没有答案