可以缓存视频吗?IOS - 斯威夫特 [英] Is it possible to cache Videos? IOS - Swift

查看:25
本文介绍了可以缓存视频吗?IOS - 斯威夫特的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Instagram、vine 甚至 facebook 等 tableView 中下载和播放视频.

I am trying to download and play videos in a tableView like Instagram, vine or even facebook.

我想要实现的是一个 tableView,我可以在其中显示视频,并在滚动时自动下载和播放.喜欢 Instagram...

What I am trying to achieve is a tableView where I display the videos and they auto download and play while scrolling. Like Instagram...

到目前为止,我已经管理了大部分,但我想改变的是,每次我查看一个单元格时,视频都会一次又一次地下载......当然必须有一种方法来缓存视频,或者只下载一次相同的视频......就像你对图像使用 SDWebImages 所做的一样.

So far I have managed most of that, but what I would like to change is the fact that every time I view a cell the video gets downloaded again and again.... Surely there must be a way to cache videos, or only download the same video once.... Like you do with SDWebImages for images.

现在每次查看单元格时都会下载它,滚动很糟糕,你可以想象.

Also at the moment with it download every time I view the cell, the scrolling is terrible as you can imagine.

现在我似乎无法弄清楚 Instagram 是如何做到的,但我 100% 确定他们不会多次下载同一个视频!!

Now I cannot seem to figure out how Instagram does it, but I am 100% sure they don't download the same video more than once!!

如果有人有建议或想法,我很想听听!!

If anyone has and advice or ideas, I'd love to hear them!!

非常感谢.

推荐答案

使用 Haneke,我无法检索缓存视频的文件路径.我通过将视频保存在缓存目录中来处理它.

Using Haneke, I wasn't able to retrieve file path for cached video. I handled it by saving video in cached directory.

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 {
                    completionHandler(Result.failure(NSError.errorWith(text: "Can't download video")))
                }
            }
        }
    }

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

        let fileURL = URL(string: stringUrl)!.lastPathComponent

        let file = self.mainDirectoryUrl.appendingPathComponent(fileURL)

        return file
    }
}

此类的示例用法如下所示:

Sample usage of this class looks like this:

CacheManager.shared.getFileWith(stringUrl: "http://techslides.com/demos/sample-videos/small.mp4") { result in

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

这篇关于可以缓存视频吗?IOS - 斯威夫特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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