Swift 2 - 与 UIActivityViewController 分享视频

Swift 2 - Share video with UIActivityViewController

我正在尝试使用 UIActivityViewController 分享来自 URL 的视频。
如果我尝试使用图像,我没有问题。

分享图片作品:

let imageThump = NSURL(string: "https://example.com/image.png")
let imageData = NSData(contentsOfURL: imageThump!) 
let objectsToShare = [comment!, imageData!]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

无法共享视频。为什么?

let videoShare = NSURL(string: "https://example.com/video.mp4")
let videoData = NSData(contentsOfURL: videoShare!) 
let objectsToShare = [comment!, videoData!]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

它不起作用,因为此 class 不允许将 NSData 传递给 Facebook activity 类型。

"UIActivityTypePostToFacebook The object posts the provided content to the user’s wall on Facebook.

When using this service, you can provide NSString, NSAttributedString, UIImage, ALAsset, and NSURL objects as data for the activity items. You may also specify NSURL objects whose contents use the assets-library scheme."

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIActivity_Class/index.html#//apple_ref/doc/constant_group/Built_in_Activity_Types

我也一直在努力让它发挥作用。还没有弄清楚如何让它工作;但是,这就是您的代码无法正常工作的原因。

这就是你的做法...

guard let urlData = NSData(contentsOfURL: NSURL(string:"https://example.com/video.mov)")!) else { 
    return 
}

print(urlData)


let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let docDirectory = paths[0]
let filePath = "\(docDirectory)/tmpVideo.mov"
urlData?.writeToFile(filePath, atomically: true)
// File Saved

let videoLink = NSURL(fileURLWithPath: filePath)


let objectsToShare = [videoLink] //comment!, imageData!, myWebsite!]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

activityVC.setValue("Video", forKey: "subject")


// New Excluded Activities Code
if #available(iOS 9.0, *) {
    activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypeOpenInIBooks, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint]
} else {
    // Fallback on earlier versions
    activityVC.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard, UIActivityTypeMail, UIActivityTypeMessage, UIActivityTypePostToTencentWeibo, UIActivityTypePostToVimeo, UIActivityTypePostToWeibo, UIActivityTypePrint ]
}

           
self.presentViewController(activityVC, animated: true, completion: nil)

请记住,视频通常是一个大文件,因此下载和保存它需要一些时间。在这里,我使用了第一个答案的一部分,但添加了一些 GCD。你会得到更好的结果。

我就是这样做的,以避免应用程序在下载并保存视频之前保持冻结状态:

let url = NSURL(string: "https://example.com/video.mov")
    
//Show activity indicator
    
DispatchQueue.global(qos: .background).async {
    guard let urlData = NSData(contentsOf: url) else { return }
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let filePath="\(documentsPath)/tempFile.mov"
    DispatchQueue.main.async {
        urlData.write(toFile: filePath, atomically: true)
                                        
        //Hide activity indicator
    
        let activityVC = UIActivityViewController(activityItems: [NSURL(fileURLWithPath: filePath)], applicationActivities: nil)
        activityVC.excludedActivityTypes = [.addToReadingList, .assignToContact]
        self.present(activityVC, animated: true, completion: nil)
    }
}