gpt4 book ai didi

ios - 带出队的 UICollectionView 查询

转载 作者:行者123 更新时间:2023-11-28 05:30:58 25 4
gpt4 key购买 nike

我有一个制作网格的 UICollectionView。构成网格的每个单元格中都有一个 UIImage(在 IB 中创建)。

我正在使用可重复使用的单元格来减少请求。我怎样才能使用这个单元格和 UIImage ?有没有办法在它消失之前将它存储在一个数组中?我已经创建了一个标签,但我没有这样做是否有帮助?如果我手动创建每个单元格,那么我的 Controller 中将有大约 100 个 @IBOutlets !!这是我显示单元格的代码..

任何想法都会很棒。我正在尝试获取单元格内的 UIImage,因此我可以隐藏它并在单元格出队之前命名它。

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("LetterCell", forIndexPath: indexPath) as UICollectionViewCell

cell.tag = indexPath.row
return cell
}

最佳答案

图像占用的内存量惊人。因此,您通常不希望架构要求您一次将所有图像(或更糟的是,单元格)保存在内存中。您希望您的 Collection View 单元格被重用,并且您希望以即时方式(也称为“惰性”图像加载)从持久存储中检索图像。

为了最大限度地减少应用程序的内存占用,您的模型通常会包含最少的信息量,例如仅对这些图像的引用(例如文件名)。仅在 UI 真正需要时才加载图像。

例如,假设图像是设备“Application Support”文件夹中的文件,那么您可能有一个文件名数组(在我下面的示例中称为 imageNames),并且您可能会做类似的事情:

var imageNames = [String]()   // this is populated elsewhere, perhaps `viewDidLoad`

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

let imageURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent(imageNames[indexPath.item])

cell.imageView.image = UIImage(contentsOfFile: imageURL.path)

return cell
}

如果您真的想将这些图像保存在内存中(例如,为了更流畅的响应时间),您可以使用 NSCache,但要确保此缓存在收到内存压力时自行清空。例如:

var imageCache = ImageCache()

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

let imageName = imageNames[indexPath.item]
if let image = imageCache[imageName] {
cell.imageView.image = image
} else {
let imageURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent(imageName)
let image = UIImage(contentsOfFile: imageURL.path)
imageCache[imageName] = image
cell.imageView.image = image
}

return cell
}

在哪里

class ImageCache: NSCache<NSString, UIImage> {

var observer: NSObjectProtocol?

override init() {
super.init()

// empty queue upon memory pressure

observer = NotificationCenter.default.addObserver(forName: UIApplication.didReceiveMemoryWarningNotification, object: nil, queue: .main) { [unowned self] notification in
self.removeAllObjects()
}
}

deinit {
NotificationCenter.default.removeObserver(observer!)
}

subscript(key: String) -> UIImage? {
get { return object(forKey: key as NSString) }
set { setValue(newValue, forKey: key) }
}
}

还可以考虑其他优化。例如,如果这些图像很大,您可能会确保加载 ImageView 时图像的大小已调整为最适合 Collection View 单元格的大小。但希望这能说明在 UICollectionView 中处理图像时的一些基本概念。

关于ios - 带出队的 UICollectionView 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28483621/

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