gpt4 book ai didi

ios - 在 UI Collection View 中滚动时,单元格会重新加载错误的图像

转载 作者:行者123 更新时间:2023-11-28 12:31:15 26 4
gpt4 key购买 nike

我正在使用 DropBox 的 API 下载图像并在 Collection View 中显示它们。当用户滚动时,图像要么从单元格中消失并加载另一个图像,要么重新加载新图像并替换单元格中的图像。我怎样才能防止这种情况发生?我试过使用 SDWebImage,这使图像保持正确的顺序,但图像仍然会消失并在每次滚动出屏幕时重新加载。此外,我直接下载图像,而不是从 URL 下载图像,我宁愿不必编写工作来使用 SDWebImage。

我会发布一个 gif 作为示例,但我的声誉太低了。

欢迎任何帮助:)

var filenames = [String]()
var selectedFolder = ""

// image cache
var imageCache = NSCache<NSString, UIImage>()

override func viewDidLoad() {
super.viewDidLoad()

getFileNames { (names, error) in
self.filenames = names
if error == nil {
self.collectionView?.reloadData()
print("Gathered filenames")
}
}

collectionView?.collectionViewLayout = gridLayout
collectionView?.reloadData()

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)

}


func getFileNames(completion: @escaping (_ names: [String], _ error: Error?) -> Void) {
let client = DropboxClientsManager.authorizedClient!
client.files.listFolder(path: "\(selectedFolder)", recursive: false, includeMediaInfo: true, includeDeleted: false, includeHasExplicitSharedMembers: false).response { response, error in
var names = [String]()
if let result = response {
for entry in result.entries {
if entry.name.hasSuffix("jpg") {
names.append(entry.name)

}
}
} else {
print(error!)
}
completion(names, error as? Error)
}
}



func checkForNewFiles() {
getFileNames { (names, error) in
if names.count != self.filenames.count {
self.filenames = names
self.collectionView?.reloadData()
}
}
}

func downloadFiles(fileName: String, completion:@escaping (_ image: UIImage?, _ error: Error?) -> Void) {

if let cachedImage = imageCache.object(forKey: fileName as NSString) as UIImage? {
print("using a cached image")
completion(cachedImage, nil)
} else {
let client = DropboxClientsManager.authorizedClient!
client.files.download(path: "\(selectedFolder)\(fileName)").response { response, error in
if let theResponse = response {
let fileContents = theResponse.1
if let image = UIImage(data: fileContents) {
// resize the image here and setObject the resized Image to save it to cache.
// use resized image for completion as well
self.imageCache.setObject(image, forKey: fileName as NSString)
completion(image, nil) // completion(resizedImage, nil)

}
else {
completion(nil, error as! Error?)
}

} else if let error = error {
completion(nil, error as? Error)
}
}
.progress { progressData in

}
}


}



override func numberOfSections(in collectionView: UICollectionView) -> Int {

return 1
}


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return self.filenames.count
}


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImageCell
cell.backgroundColor = UIColor.lightGray

let fileName = self.filenames[indexPath.item]
let cellIndex = indexPath.item
self.downloadFiles(fileName: fileName) { (image, error) in
if cellIndex == indexPath.item {
cell.imageCellView.image = image
print("image download complete")

}
}

return cell
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
gridLayout.invalidateLayout()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
imageCache.removeAllObjects()
}

最佳答案

因为 TableView 和 CollectionView 使用

dequeueReusableCell(withReuseIdentifier: for indexPath:) 函数 当你配置一个新的单元格时,swift 在表格下面做的是使用屏幕外的单元格来帮助你的手机内存和可能那个单元格已经有一个图像集,你必须处理这种情况。

在这种情况下,我建议您查看方法“prepareCellForReuse”,我认为您需要做的是将 imageView.image 属性设置为 nil。

我很确定它会解决您的问题或给您正确的方向,但如果它不起作用请告诉我,我会尽力帮助您。

最好的结果。

关于ios - 在 UI Collection View 中滚动时,单元格会重新加载错误的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com