Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView中出现缩略图错误_Ios_Swift_Uitableview_Thumbnails - Fatal编程技术网

Ios UITableView中出现缩略图错误

Ios UITableView中出现缩略图错误,ios,swift,uitableview,thumbnails,Ios,Swift,Uitableview,Thumbnails,我正在使用以下步骤编写代码,以获取youtube视频缩略图。缩略图url正在打印,但我也收到了以下提到的错误: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID") as! VideoAdsCell l

我正在使用以下步骤编写代码,以获取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
}
我将此功能用于视频缩略图。但是我得到了错误

图像url:

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

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

如果您使用的是未提供视频缩略图url的
youtube
API

我可以看到你传递给获取缩略图的URL已经是一个缩略图URL,所以你不需要做任何额外的事情。错误原因与将图像url传递到
avurlaste
时相同,后者需要视频url。所以它不能打开

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

例如:


您不应该向应该使用从该URL获取的资源的初始值设定项提供网络URL。网络请求应该异步执行,而初始值设定项必须以同步方式返回,因此在向需要本地URL的方法提供远程URL时,您会自找麻烦

此外,如果您只想下载图像并将其显示为
UIImage
,则无需使用
avurlaste
。您只需使用
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
}

这是可行的,但缩略图是模糊类型。在这种情况下,
层的设置有缺陷。图像本身只需使用
UIImage下载。从RemoteURL下载,不需要对其进行任何修改。我建议你打开一个新的问题来修复图像的外观,因为这是一个不同于你在这个问题上的问题。你还应该包括缩略图预期/实际外观的屏幕截图。如果是视频,你应该使用AVAssetImageGenerator,否则你需要下载并显示图像
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
}