xcode Swift 2 - 与 UIActivityViewController 共享视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33618673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:11:00  来源:igfitidea点击:

Swift 2 - Share video with UIActivityViewController

xcodefacebookswift2socialuiactivityviewcontroller

提问by SNos

I am trying to share a video from a URL using UIActivityViewController. If I try with an Image I have not problems.

我正在尝试使用UIActivityViewController. 如果我尝试使用图像,我没有问题。

Share Image Works:

分享图片作品:

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)

Share Video is not working. Why?

共享视频不起作用。为什么?

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)

回答by SNos

THIS IS HOW YOU DO IT....

这就是你的方式......

  let urlData = NSData(contentsOfURL: NSURL(string:"https://example.com/video.mov)")!)

            if ((urlData) != nil){

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

回答by Noel C. Perez

Remember that a video is usually a big file, so it'll take some time for it to be downloaded and saved. Here I used part of the first answer but adding some GCD. You'll get better results.

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

This is how I did it to avoid the App to stay frozen until the video is downloaded and saved:

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

    let url = NSURL(string:"https://example.com/video.mov)"

    //Show activity indicator

     DispatchQueue.global(qos: .background).async {
         if let urlData = NSData(contentsOf: url){
           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)
              }
          }
      }

回答by Eric Mentele

It is not working because this class does not allow NSData to be passed to the Facebook activity type.

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

"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."

"UIActivityTypePostToFacebook 对象将提供的内容发布到用户在 Facebook 的墙上。

使用此服务时,您可以提供 NSString、NSAttributedString、UIImage、ALAsset 和 NSURL 对象作为活动项的数据。您还可以指定其内容使用资产库方案的 NSURL 对象。”

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

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

I have been trying to get this to work as well. Haven't figured out how to get it to work; however, this is why your code is not working.

我也一直在努力让它发挥作用。还没想好如何让它工作;但是,这就是您的代码不起作用的原因。