- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个制作网格的 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/
我的 JavaScript 代码中有一道数学题。我需要将给定数量的玩家随机分成 2 队,这样每次 - 如果玩家想再次比赛 - 团队都会重新组成,并且在形成所有组合之前他们应该不同。 假设我有 4 个玩
我是一名优秀的程序员,十分优秀!