情况将 url 图像设置为 UITableViewCell 时出现问题。我创建了 CancelableImageView,它是自定义 UIImageView,用于在有新图像时取消图像下载和设置任务……
将 url 图像设置为 UITableViewCell 时出现了问题。我创建了自定义 CancelableImageView
, UIImageView
当设置新图像时取消图像下载和设置任务。使用此方法,“错误图像”问题似乎已解决,但这仍然发生在 UITableView 上
加载 UITableView
后 第一个单元格 完成加载图像并更改图像后, 第四个单元格 的图像也更改为 第一个单元格的图像 。 滚动后,第四个单元格的图像更改为正确的图像。
这是我的 UIImage 图像加载扩展(包含图像缓存)
/// Singleton for Image Caching
class ImageCacheManager {
/// Storage to save cached UIImage
private let cache = Cache<String, UIImage>()
/// singleton instance
static let shared = ImageCacheManager()
/**
Get image from cache data for url String
- Parameter key: String url of UIImage
- Returns: Retrun cached image for url. Retrun nil when cached image is not exist
*/
func loadCachedData(for key: String) -> UIImage? {
let itemURL = NSString(string: key)
return cache.value(forKey: key)
}
/**
Save UIImage to cache data
- Parameters:
- image: UIImage to save in cache data
- key: String url of UIImage
*/
func saveCacheData(of image: UIImage, for key: String) {
let itemURL = NSString(string: key)
cache.insertValue(image, forKey: key)
}
}
extension UIImageView {
/**
Set image to UIImageView with Cached Image data or data from URL
- Parameters:
- urlString: String url of image
- forceOption: Skip getting image from cache data and force to get image from url when true. default false
*/
func loadImage(_ urlString: String?, forceOption: Bool = false) -> UIImage? {
guard let imagePath = urlString else { return nil }
// Cached Image is available
if let cachedImage = ImageCacheManager.shared.loadCachedData(for: imagePath), forceOption == false {
return cachedImage
// No Image Cached
} else {
guard let imageURL = URL(string: imagePath) else { return nil }
guard let imageData = try? Data(contentsOf: imageURL) else { return nil }
guard let newImage = UIImage(data: imageData) else { return nil }
ImageCacheManager.shared.saveCacheData(of: newImage, for: imagePath)
return newImage
}
}
func setImage(_ urlString: String?, forceOption: Bool = false) {
DispatchQueue.global().async {
guard let image = self.loadImage(urlString, forceOption: forceOption) else { return }
DispatchQueue.main.async {
self.image = image
}
}
}
}
这是自定义 UIImage,当设置新图像时取消图像加载任务:
/// UIImageView with async image loading functions
class CancelableImageView: UIImageView {
/// Cocurrent image loading work item
private var imageLoadingWorkItem: DispatchWorkItem?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init() {
super.init(frame: CGRect.zero)
}
}
// MARK: Concurrent image loading method
extension CancelableImageView {
/**
Cancel current image loading task and Set image to UIImageView with Cached Image data or data from URL
- Parameters:
- urlString: String url of image
- forceOption: Skip getting image from cache data and force to get image from url when true. default false
*/
func setNewImage(_ urlString: String?, forceOption: Bool = false) {
self.cancelLoadingImage()
self.imageLoadingWorkItem = DispatchWorkItem { super.setImage(urlString, forceOption: forceOption) }
self.imageLoadingWorkItem?.perform()
}
/// Cancel current image loading work
func cancelLoadingImage() {
DispatchQueue.global().async {
self.imageLoadingWorkItem?.cancel()
}
}
}
这是该视图的 UITableViewCell:
class ChartTableViewCell: UITableViewCell {
lazy var posterImageView = CancelableImageView().then {
$0.contentMode = .scaleAspectFit
$0.image = UIImage(named: "img_placeholder")
}
...
override func prepareForReuse() {
setPosterPlaceholderImage()
super.prepareForReuse()
}
}
extension ChartTableViewCell {
/// Set poster imageView placeholder Image
private func setPosterPlaceholderImage() {
self.posterImageView.image = UIImage(named: "img_placeholder")
}
// MARK: Set Data
func setData(rank: Int, movie: MovieFront) {
rankLabel.text = "\(rank+1)"
titleLabel.text = movie.title
genreLabel.text = movie.genre
releaseDateLabel.text = movie.releaseDate
starRating.rating = movie.ratingScore/2
ratingCountLabel.text = "(\(movie.ratingCount))"
if let imagePath = movie.posterPath {
posterImageView.setNewImage(APIService.configureUrlString(imagePath: imagePath))
}
}
}
这是 UITableViewDataSource (cellForRowAt):
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: identifiers.chart_table_cell) as? ChartTableViewCell else {
return UITableViewCell()
}
cell.setData(rank: indexPath.row, movie: viewModel.movieListData.value[indexPath.row])
return cell
}