使用UIActivityViewController分享视频

15

我正在开发一个应用程序,希望使用uiactivityviewcontroller共享保存在我的文档目录中的图像、视频和其他文件。 图像和文件共享正常工作。 图像共享的代码:

var textToShare : Any!
textToShare = UIImage(data:data)//data of an image

代码针对存储在文档目录中的其他文件。

var textToShare : Any!
textToShare = url//url of file saved into document directory

但是我不知道如何分享视频。在此之后,我使用以下代码来使用activityviewcontroller。

let activityViewController = UIActivityViewController(activityItems: [textToShare], applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

    // exclude some activity types from the list (optional)
    activityViewController.excludedActivityTypes = [ UIActivityType.airDrop, UIActivityType.postToFacebook ]

    // present the view controller
    self.present(activityViewController, animated: true, completion: nil)
4个回答

17

首先获取视频文件路径

 let videoURL = NSURL(fileURLWithPath:localVideoPath)

然后将此路径传递给UIActivityViewController,如下所示

let activityItems = [videoURL, "Check this out!" ]
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

activityController.popoverPresentationController?.sourceView = self.view
activityController.popoverPresentationController?.sourceRect = self.view.frame

self.presentViewController(activityController, animated: true, completion: nil)

更新了适用于Swift 4的代码

    let localVideoPath = "your_video_path_here..."
    let videoURL = URL(fileURLWithPath: localVideoPath)

    let activityItems: [Any] = [videoURL, "Check this out!"]
    let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

    activityController.popoverPresentationController?.sourceView = view
    activityController.popoverPresentationController?.sourceRect = view.frame

    self.present(activityController, animated: true, completion: nil)

我已经有视频链接,但它没有存储在本地路径中。它保存在图库中。我需要先导出它。你能解释一下如何导出视频吗?我已经尝试过了,但无法分享视频。 - Aqeel Ahmad
2
没问题,可以使用相册提供的路径。您正在使用UIImagePickerController吗? - Abdul Yasin
目前我正在使用Phasset获取存储在相册中的所有视频的URL。将来,我考虑切换到UiImagePickerController,但现在我只有视频的URL,并且想要导出它。如果只使用URL无法导出,请告诉我。我会更改我的代码以使用UiImagePickerController。谢谢 :) - Aqeel Ahmad
我已经将我的代码更改为uiImagePicker,你能告诉我现在如何分享它吗? - Aqeel Ahmad
2
你的方法适用于UIImagePickerController,谢谢 :) - Aqeel Ahmad

2

Swift 5.0 // 使用FileManager和文件名的方法

  let localVideoPath = "video_file_name.mp4" // NAME OF THE VIDEO FILE
                
    if let dir = try? FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) {
                   
      let videoURL = URL(fileURLWithPath: dir.absoluteString).appendingPathComponent(localVideoPath)

      let activityItems: [Any] = [videoURL]
      let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

      activityController.popoverPresentationController?.sourceView = view
      activityController.popoverPresentationController?.sourceRect = view.frame
      self.present(activityController, animated: true, completion: nil)
   }
                    

2
从本地文档目录路径分享视频
func shareVideo(videoPath : String){
        let localVideoPath = videoPath
        let videoURL = URL(fileURLWithPath: localVideoPath)

        let activityItems: [AnyObject] = [videoURL as AnyObject]
        let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)

        activityController.popoverPresentationController?.sourceView = view
        activityController.popoverPresentationController?.sourceRect = view.frame

        self.present(activityController, animated: true, completion: nil)
    }

0
func shareUsingActivity() {

    let yourPath = ""
    let yourUrl = URL(fileURLWithPath: yourPath)
    let activity: [Any] = [yourUrl, "Your custom message here…"]
    let actController = UIActivityViewController(activity: activity, applicationActivities: nil)
    actController.popoverPresentationController?.sourceView = view
    actController.popoverPresentationController?.sourceRect = view.frame
    self.present(actController, animated: true, completion: nil)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接