当前位置: 首页 > 面试题库 >

Swift-使用downloadTaskWithURL下载视频

奚修伟
2023-03-14
问题内容

感谢downloadTaskWithURL,我正在下载视频,并使用以下代码将其保存到我的画廊:

    func saveVideoBis(fileStringURL:String){

    print("saveVideoBis");

    let url = NSURL(string: fileStringURL);
    (NSURLSession.sharedSession().downloadTaskWithURL(url!) { (location:NSURL?, r:NSURLResponse?, e:NSError?) -> Void in

        let mgr = NSFileManager.defaultManager()

        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0];

        print(documentsPath);

        let destination = NSURL(string: NSString(format: "%@/%@", documentsPath, url!.lastPathComponent!) as String);

        print(destination);

        try? mgr.moveItemAtPath(location!.path!, toPath: destination!.path!)

        PHPhotoLibrary.requestAuthorization({ (a:PHAuthorizationStatus) -> Void in
            PHPhotoLibrary.sharedPhotoLibrary().performChanges({
                PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(destination!);
                }) { completed, error in
                    if completed {
                        print(error);
                        print("Video is saved!");
                        self.sendNotification();
                    }
            }
        })
    }).resume()
}

它在我的模拟器上可以正常工作,但是在我的iPad上,即使print("Video is saved!");出现视频也不会保存视频。你知道为什么吗?

我的控制台中也出现了该消息

无法从文件创建数据(空)


问题答案:

请通过代码检查注释:

Xcode 8•Swift 3

import UIKit
import Photos

class ViewController: UIViewController {

    func downloadVideoLinkAndCreateAsset(_ videoLink: String) {

        // use guard to make sure you have a valid url
        guard let videoURL = URL(string: videoLink) else { return }

        guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

        // check if the file already exist at the destination folder if you don't want to download it twice
        if !FileManager.default.fileExists(atPath: documentsDirectoryURL.appendingPathComponent(videoURL.lastPathComponent).path) {

            // set up your download task
            URLSession.shared.downloadTask(with: videoURL) { (location, response, error) -> Void in

                // use guard to unwrap your optional url
                guard let location = location else { return }

                // create a deatination url with the server response suggested file name
                let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggestedFilename ?? videoURL.lastPathComponent)

                do {

                    try FileManager.default.moveItem(at: location, to: destinationURL)

                    PHPhotoLibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> Void in

                        // check if user authorized access photos for your app
                        if authorizationStatus == .authorized {
                            PHPhotoLibrary.shared().performChanges({
                                PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed, error in
                                    if completed {
                                        print("Video asset created")
                                    } else {
                                        print(error)
                                    }
                            }
                        }
                    })

                } catch { print(error) }

            }.resume()

        } else {
            print("File already exists at destination url")
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()
        downloadVideoLinkAndCreateAsset("https://www.yourdomain.com/yourmovie.mp4")
    }

}


 类似资料:
  • 问题内容: 嗨,我有一堆.mp3文件,我想与NSFileManager一起使用并存储在documents文件夹中。有没有一种方法可以在线下载.mp3文件,然后将其保存到documents文件夹?这就是我正在使用的本地文件。 问题答案: 编辑/更新: Xcode 11.5•Swift 5.2 原始答案 Xcode 8.3.2•Swift 3.1

  • 在视频影像商店可购买或租赁视频影像。 可通过与「下载内容」相同的方法下载内容。已下载的内容可进入(视频影像)观赏。 若要使用(PS Store),需先注册PlayStation®Network账号。即使未使用PS Vita注册PlayStation®Network也可阅览PlayStation®Store。下载内容时需先注册,请遵循显示的画面注册。 PlayStation®Network和Play

  • 本文向大家介绍python 视频下载神器(you-get)的具体使用,包括了python 视频下载神器(you-get)的具体使用的使用技巧和注意事项,需要的朋友参考一下 you-get是github上python的一个开源库(https://github.com/soimort/you-get),使用you-get你只需要取得视频所在网页链接地址就可以很轻松的下载下来,目前you-get所支持的

  • 问题内容: new YouTubePageStreamUriGetter().execute( 该代码不起作用 问题答案: 编辑3 您可以使用Lib:https : //github.com/HaarigerHarald/android- youtubeExtractor 例如: 他们使用以下方法解密Signature: 现在,使用此库, 高质量视频会 丢失 音频,因此我将MediaMuxer用于

  • 问题内容: 我想在特定时间从视频中抓取帧。我正在将我的抓取帧函数调用为Float64,将时间指定为秒。问题在于它没有抓取当前帧。似乎忽略了小数点。如果我用例如1.22和1.70调用该函数,它将返回同一帧。关于Swift,我是一个新手,所以我想我没有正确使用CMTime对象。那么,谁能看到这是怎么回事? 问题答案: 我设法找出添加: …对我的职能将达到目的。 我更新的函数如下所示:

  • 问题内容: 我有一个应用程序,该应用程序在视图的下半部分具有文本字段。这意味着当我输入文本字段时,键盘将覆盖文本字段。 如何在键入时向上移动视图,以便可以看到正在键入的内容,然后在键盘消失时将其向下移动到原始位置? 我到处都看过,但是所有解决方案似乎都在Obj-C中,我还不能完全转换。 任何帮助将不胜感激。 问题答案: 这是一个解决方案,无需处理从一个textField到另一个的切换: 要解决此问