如何异步下载和缓存要在我的应用中使用的视频? [英] How do I asynchronously download and cache videos for use in my app?

查看:203
本文介绍了如何异步下载和缓存要在我的应用中使用的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know that SDWebImage loads the image in a background thread so you're not blocking the UI/main thread when this downloading is going on. Furthermore, it will also disk-cache all the images you've downloaded and will NEVER re-download an image from the same URL.

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

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

Something to note: I add Videos as Sublayer.

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 in the past but it seems like it does something different or at the very leased has too much extra functionality I dont need.

更新:

我正在测试的内容:

  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.

I do not understand however if I should do something right after the last line or if I should do it after the }) or if I need to add a Update UI there.

推荐答案

因此,我可以通过以下方法解决问题:

So I was able to solve the problem with the following:

快捷键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;
        }
    }

这篇关于如何异步下载和缓存要在我的应用中使用的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆