UITableView 出现缩略图错误
Thumbnail error occurred in UITableView
我正在按照这些步骤编写代码以获取 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: 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
}
}
如果您使用的 youtube
API 没有为您提供视频缩略图 url?
我可以看到您传递给获取缩略图的 URL 已经是缩略图 url 所以您不需要做任何额外的事情。原因是您的错误与您将图像 url 传递给 AVURLAsset
相同,而后者需要视频 url。所以打不开
您只需要使用 SDWebImage 或任何其他第三方库即可在 imageView
上显示图像。它自己处理缓存和异步下载。
SDWebImage 示例:
cell.imageForMain.sd_setImage(with: URL(string: "http://img.youtube.com/vi/\(youtubeId!)/1.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
您永远不应该向应该使用从 URL 获取的资源的初始化程序提供网络 URL。网络请求应该异步执行,而初始化程序必须 return 以同步方式执行,因此在向需要本地 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
}
我正在按照这些步骤编写代码以获取 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: 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
}
}
如果您使用的 youtube
API 没有为您提供视频缩略图 url?
我可以看到您传递给获取缩略图的 URL 已经是缩略图 url 所以您不需要做任何额外的事情。原因是您的错误与您将图像 url 传递给 AVURLAsset
相同,而后者需要视频 url。所以打不开
您只需要使用 SDWebImage 或任何其他第三方库即可在 imageView
上显示图像。它自己处理缓存和异步下载。
SDWebImage 示例:
cell.imageForMain.sd_setImage(with: URL(string: "http://img.youtube.com/vi/\(youtubeId!)/1.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
您永远不应该向应该使用从 URL 获取的资源的初始化程序提供网络 URL。网络请求应该异步执行,而初始化程序必须 return 以同步方式执行,因此在向需要本地 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
}