使用uiactivityViewcontroller分享视频

Aqe*_*eel 9 uiactivityviewcontroller swift3

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

var textToShare : Any!
textToShare = UIImage(data:data)//data of an image
Run Code Online (Sandbox Code Playgroud)

存储在文档目录中的其他文件的代码.

var textToShare : Any!
textToShare = url//url of file saved into document directory
Run Code Online (Sandbox Code Playgroud)

但我不知道如何分享视频.在此之后,我使用以下代码来使用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)
Run Code Online (Sandbox Code Playgroud)

Abd*_*sin 13

首先获取视频文件路径

 let videoURL = NSURL(fileURLWithPath:localVideoPath)
Run Code Online (Sandbox Code Playgroud)

然后将此路径传递到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)
Run Code Online (Sandbox Code Playgroud)

更新了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)
Run Code Online (Sandbox Code Playgroud)

  • 是的,那可以使用画廊提供的路径.你在使用UIImagePickerController吗? (2认同)
  • 你的方法适用于uiimagepickercontroller谢谢:) (2认同)