Swift3 使用uiactivityViewcontroller共享视频

Swift3 使用uiactivityViewcontroller共享视频,swift3,uiactivityviewcontroller,Swift3,Uiactivityviewcontroller,我正在开发一个应用程序,希望使用uiactivityviewcontroller共享保存到文档目录中的图像、视频和其他文件。图像和文件共享工作正常。 图像共享代码 var textToShare : Any! textToShare = UIImage(data:data)//data of an image 存储在文档目录中的其他文件的代码 var textToShare : Any! textToShare = url//url of file saved into document di

我正在开发一个应用程序,希望使用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)

首先获取视频文件路径

 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的更新代码


从本地文档目录路径共享视频

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

带FileManager和文件名的Swift 5.0//方法

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

我已经有视频url,但它不是存储在本地路径。它被保存到库中。我需要先把它导出。你能解释一下如何导出视频吗??我已经试过了,但无法共享视频。是的,使用gallery提供的路径可以。你正在使用UIImagePickerController吗?不,现在我正在使用Phaset获取存储在gallery中的所有视频的url。将来我想打开UiImagePickerController,但现在我只有视频的url,我想导出它。如果无法仅使用url导出,请告诉我。我将更改代码以使用UiImagePickerController。答:)我已将代码更改为uiImagePicker,您能告诉我现在如何共享它吗。您的方法适用于uiimagepickercontroller谢谢:)
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)
}
  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)
   }