如何异步下载和缓存要在我的应用中使用的视频?

时间:2019-04-23 20:34:12

标签: ios swift asynchronous firebase-realtime-database

我知道SDWebImage将图像加载到后台线程中,因此在进行下载时您不会阻塞UI /主线程。此外,它还将磁盘缓存您已下载的所有图像,并且永远不会从同一URL重新下载图像。

所以我想知道视频中是否有相似或相同的东西?

注意事项:我将视频添加为子层。

let videoURL = URL(string: postArray[indexPath.item].media[0].videoURLString!)//need to do error handlin here
print(videoURL as Any, "<-- video url in dispkay")

let player = AVPlayer(url: videoURL! as URL)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRect(x: -8, y: 0, width: 138, height: 217)//cell.frame


cell.imageOrVideoView.layer.addSublayer(playerLayer)
//Other code and play()

This was recommended过去,但它似乎做了一些不同的事情,或者说租约上有我不需要的太多额外功能。

更新

我正在测试的内容:

  DispatchQueue.global(qos: .default).async(execute: {

            var downloadedData: Data? = nil
            if let url = URL(string: videoURL) {
                do {
                    downloadedData = try Data(contentsOf: url)
                } catch {
                    print(error, "downloaded Data failed")
                }
            }

            if downloadedData != nil {

                // STORE IN FILESYSTEM
                var cachesDirectory = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)[0]
                var file = URL(fileURLWithPath: cachesDirectory).appendingPathComponent(videoURL).absoluteString
                do {
                    try downloadedData?.write(to: URL(string: file)!)
                } catch {
                    print(error, "error dowloading data and writing it")
                }

                // STORE IN MEMORY
                if let downloadedData = downloadedData {
                    memoryCache?.setObject(downloadedData as AnyObject, forKey: videoURL as AnyObject)
                }
            }

            // NOW YOU CAN CREATE AN AVASSET OR UIIMAGE FROM THE FILE OR DATA
        })

但是,我不知道是应该在最后一行之后还是在})之后执行操作,还是需要在其中添加更新UI。

1 个答案:

答案 0 :(得分:6)

所以我可以通过以下方法解决问题:

快捷键4:

import Foundation

public enum Result<T> {
    case success(T)
    case failure(NSError)
}

class CacheManager {

    static let shared = CacheManager()
    private let fileManager = FileManager.default
    private lazy var mainDirectoryUrl: URL = {

    let documentsUrl = self.fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!
        return documentsUrl
    }()

    func getFileWith(stringUrl: String, completionHandler: @escaping (Result<URL>) -> Void ) {

        let file = directoryFor(stringUrl: stringUrl)

        //return file path if already exists in cache directory
        guard !fileManager.fileExists(atPath: file.path)  else {
            completionHandler(Result.success(file))
            return
        }

        DispatchQueue.global().async {

            if let videoData = NSData(contentsOf: URL(string: stringUrl)!) {
                videoData.write(to: file, atomically: true)
                DispatchQueue.main.async {
                    completionHandler(Result.success(file))
                }
            } else {
                DispatchQueue.main.async {
                    let error = NSError(domain: "SomeErrorDomain", code: -2001 /* some error code */, userInfo: ["description": "Can't download video"])

                    completionHandler(Result.failure(error))
                }
            }
        }
    }

    private func directoryFor(stringUrl: String) -> URL {

        let fileURL = URL(string: stringUrl)!.lastPathComponent
        let file = self.mainDirectoryUrl.appendingPathComponent(fileURL)
        return file
    }
}

用法:

      CacheManager.shared.getFileWith(stringUrl: videoURL) { result in

        switch result {
        case .success(let url):
        // do some magic with path to saved video

            break;
        case .failure(let error):
            // handle errror

            print(error, " failure in the Cache of video")
            break;
        }
    }