使用uiactivityViewcontroller分享视频

时间:2017-08-23 09:36:08

标签: 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 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 个答案:

答案 0 :(得分:12)

首先获取视频文件路径

 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)

答案 1 :(得分:1)

要共享本地文档目录路径中的视频

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

lat      long
31.76    77.84
31.77    77.84
31.78    77.84
32.76    77.85