如何在tableview列表上异步加载视频网址缩略图

时间:2017-10-27 09:30:27

标签: swift list asynchronous tableview

我做了一个演示,在视频网址上加载了一堆缩略图。现在我想在我的tableview列表中显示所有已加载的缩略图,但问题是那个.tablview被绞死直到所有下载的缩略图图像,如果我使用异步线程比我的tableview工作正常(滚动)但所有缩略图图像在20到25秒后加载。

这是我的代码

@IBOutlet weak var tableviewList:UITableView!
    var listArray:[String] = ["https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
                              "http://techslides.com/demos/sample-videos/small.mp4",
                              "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
                              "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
                              "http://techslides.com/demos/sample-videos/small.mp4",
                              "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v",
                              "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4",
                              "http://techslides.com/demos/sample-videos/small.mp4",
                              "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v"
                                ]

    override func viewDidLoad() {

        super.viewDidLoad()
        self.tableviewList.register(UINib(nibName: "ListTableViewCell", bundle: nil), forCellReuseIdentifier: "ListTableViewCell")

        for aryString in self.listArray
        {
            self.createThumbnailOfVideoFromFileURL(aryString)
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
            return self.listArray.count
    }

    public  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ListTableViewCell", for: indexPath) as! ListTableViewCell

        let strUrl = self.listArray[indexPath.row]

        DispatchQueue.global(qos: .userInitiated).async {
            // Download file or perform expensive task
            cell.imageviewThumb?.image =   self.getThumbnailFrom(path: URL.init(string:strUrl )!)
            DispatchQueue.main.async {
                // Update the UI
            }
        }

        cell.selectionStyle = UITableViewCellSelectionStyle.none

            return cell
    }

     func  tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let stringUrl:String = self.listArray[indexPath.row]
        let videoUrl = NSURL(string:stringUrl)
        let player = AVPlayer(url:videoUrl as! URL)
        let playerviewController = AVPlayerViewController()
        playerviewController.player  = player

        self.present(playerviewController, animated: true, completion: {
            playerviewController.player?.play()
        })
    }

    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
      return 300
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

     return true

    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if (editingStyle == UITableViewCellEditingStyle.delete) {
            // handle delete (by removing the data from your array and updating the tableview)
        }
    }


    func getThumbnailFrom(path: URL) -> UIImage? {
        do {

            let asset = AVURLAsset(url: path , options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
            let thumbnail = UIImage(cgImage: cgImage)
            return thumbnail

        } catch let error {
            print("*** Error generating thumbnail: \(error.localizedDescription)")
            return nil
        }
    }

请给我一些可能的解决方案,我想要像 SDWebImages 这样的东西,但它仅适用于不用于视频缩略图的图像。我想要swift 3.0解决方案。提前谢谢

1 个答案:

答案 0 :(得分:3)

试试这个

 do {
            let asset = AVURLAsset(url: url , options: nil)
            let imgGenerator = AVAssetImageGenerator(asset: asset)
            imgGenerator.appliesPreferredTrackTransform = true
            let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
            thumbnail = UIImage(cgImage: cgImage)





        } catch let error {
            print("*** Error generating thumbnail: \(error.localizedDescription)")
        }

    }
相关问题