【问题标题】:Thumbnail error occurred in UITableViewUITableView 出现缩略图错误
【发布时间】:2018-11-06 22:12:54
【问题描述】:

我正在使用这些步骤编写代码以获取 youtube 视频缩略图。缩略图 url 正在打印,但我也收到下面提到的错误:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! VideoAdsCell
    let youtubeId = extractYoutubeIdFromLink(link: arrayVideo[indexPath.row].video)

    if let url = URL(string: "http://img.youtube.com/vi/\(youtubeId!)/1.jpg") {
        print(url)

        let thumbnail = getThumbnailFrom(path: url)
        print(thumbnail as Any)

        cell.imageForMain.image = thumbnail
        cell.viewForMain.layer.masksToBounds = false
        cell.viewForMain.layer.shadowOffset = CGSize(width: -1, height: 1)
        cell.viewForMain.layer.shadowRadius = 1
        cell.viewForMain.layer.shadowOpacity = 1
        cell.viewForMain.layer.cornerRadius = 8
    }

    return cell
}

我正在为视频缩略图使用此功能。但我得到了错误。

图片网址:http://img.youtube.com/vi/XHEa9Zu9qsc/1.jpg

错误:生成缩略图时出错:无法打开

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
    }
}

【问题讨论】:

  • 如果是视频,你应该使用 AVAssetImageGenerator 否则你需要下载并显示图像

标签: ios swift uitableview thumbnails


【解决方案1】:

如果您使用的youtube API 未提供视频缩略图网址?

我可以看到您传递给获取缩略图的 URL 已经是缩略图 URL,因此您无需执行任何额外操作。原因是您的错误与您将图像网址传递给AVURLAsset 相同,而AVURLAsset 则需要一个视频网址。所以打不开。

您只需要使用SDWebImage 或任何其他第三方库即可在imageView 上显示图像。它自己处理缓存和异步下载。

SDWebImage 的示例:

cell.imageForMain.sd_setImage(with: URL(string: "http://img.youtube.com/vi/\(youtubeId!)/1.jpg"), placeholderImage: UIImage(named: "placeholder.png"))

【讨论】:

    【解决方案2】:

    您永远不应该将网络 URL 提供给应该使用从该 URL 获取的资源的初始化程序。网络请求应该异步执行,而初始化程序必须以同步方式返回,因此在向需要本地 URL 的方法提供远程 URL 时,您会遇到麻烦。

    此外,如果您只是想下载图像并将其显示为UIImage,则不需要AVURLAsset。您只需使用URLSession.dataTask 即可下载图片。

    您可以使用此UIImage 扩展来异步下载图像:

    extension UIImage {
        static func downloadFromRemoteURL(_ url: URL, completion: @escaping (UIImage?,Error?)->()) {
            URLSession.shared.dataTask(with: url) { data, response, error in
                guard let data = data, error == nil, let image = UIImage(data: data) else {
                    DispatchQueue.main.async {
                        completion(nil,error)
                    }
                    return
                }
                DispatchQueue.main.async {
                    completion(image,nil)
                }
            }.resume()
        }
    }
    

    然后用它来得到这样的缩略图:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! VideoAdsCell
        let youtubeId = extractYoutubeIdFromLink(link: arrayVideo[indexPath.row].video)
    
        if let url = URL(string: "http://img.youtube.com/vi/\(youtubeId!)/1.jpg") {
            print(url)
            UIImage.downloadFromRemoteURL(url, completion: { thumbnail,error in
                guard let thumbnail = thumbnail, error == nil else {
                    print(error)
                    return
                }
                cell.imageForMain.image = thumbnail
                cell.viewForMain.layer.masksToBounds = false
                cell.viewForMain.layer.shadowOffset = CGSize(width: -1, height: 1)
                cell.viewForMain.layer.shadowRadius = 1
                cell.viewForMain.layer.shadowOpacity = 1
                cell.viewForMain.layer.cornerRadius = 8
            }
        }
    
        return cell
    }
    

    【讨论】:

    • 这是有效的,但缩略图是模糊类型的。
    • 在这种情况下,layer 的设置存在缺陷。图像本身只是使用UIImage.downloadFromRemoteURL 下载的,没有应用任何修改。我建议您打开一个新问题来修复图像的外观,因为这与您在此问题中遇到的问题不同。您还应该包括缩略图的预期/实际外观的屏幕截图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 2012-11-21
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多