使用 uiactivityViewcontroller 共享视频
Posted
技术标签:
【中文标题】使用 uiactivityViewcontroller 共享视频【英文标题】:sharing video using uiactivityViewcontroller 【发布时间】:2017-08-23 09:36:07 【问题描述】:我正在开发一个应用程序,并希望使用 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)
【问题讨论】:
【参考方案1】:首先获取视频文件路径
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)
【讨论】:
我已经有视频 url 但它没有存储在本地路径中。它被保存到图库中。我需要先导出它。你能解释一下如何导出视频吗?我已经尝试过了,但无法分享视频。 是的,可以使用画廊提供的路径。你在用 UIImagePickerController 吗? 不,现在我正在使用 phasset 获取存储到图库中的所有视频的 url。将来我想打开 UiImagePickerController 但现在我只有视频的 url,我想导出它。如果无法仅使用 url 导出它,请告诉我。我将更改我的代码以使用 UiImagePickerController。谢谢你:) 我已将代码更改为 uiImagePicker,您能告诉我现在如何分享吗? 你的方法适用于 uiimagepickercontroller 谢谢 :)【参考方案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)
【讨论】:
【参考方案3】: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)
【讨论】:
【参考方案4】: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)
【讨论】:
以上是关于使用 uiactivityViewcontroller 共享视频的主要内容,如果未能解决你的问题,请参考以下文章
使用 UIActivityViewController 共享图像
使用 uiactivityViewcontroller 共享视频
UIActivityViewController 不使用 Facebook 登录
提示使用不使用 UIActivityViewController 保存到 iCloud