使用 uiactivityViewcontroller 分享视频
sharing video using 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)
首先获取视频文件路径
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)
从本地文档目录路径分享视频
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)
}
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)
}
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)
}
我正在开发一个应用程序,想要使用 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
的代码 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)
从本地文档目录路径分享视频
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)
}
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)
}
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)
}