【发布时间】:2016-08-24 01:23:54
【问题描述】:
我有一个包含 30 个单元格的 collectionview 的应用程序。它的单元格由图像和标签组成。我的问题是,当我加载该图像时,我的内存使用量为 87 Mb。但是如果我不设置那个图像,我的内存使用只有 30 Mb。
我正在使用Moa 异步加载图像。这是我的单元格初始化:
import UIKit
import moa
class ProductListItemCell: UICollectionViewCell {
@IBOutlet var imgProductItem: UIImageView!
@IBOutlet var titlleProductList: UILabel!
@IBOutlet var priceProductList: UILabel!
@IBOutlet var boldPriceProductList: UILabel!
func updateContent(product: Item) {
// if let imgUrl = product.picUrl {
// self.imgProductItem.moa.url = "http:\(imgUrl)"
// }
if let title = product.title {
self.titlleProductList.text = title
}
if let price = product.priceTag {
self.priceProductList.setStrikethrough(price)
}
if let boldPrice = product.price {
self.boldPriceProductList.text = boldPrice
}
}
}
这是我的cellForItemAtIndexPath
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if collectionView == myItemsCollectionView {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("firstCell", forIndexPath: indexPath) as! myCollectionViewCell
cell.updateContent(self.myItems.items[indexPath.item])
return cell
}
else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("othersCell", forIndexPath: indexPath) as! myCollectionViewCell
cell.updateContent(others[indexPath.section].items[indexPath.item])
return cell
}
}
有没有办法下载图像并“压缩”它?还是我做错了?请告诉我。在这一点上卡了一个星期。
注意:对于像我的应用程序这样带有 collectionview 的某些应用程序有多少内存使用标准? 30Mb是否属于“高”?我是内存管理的新手。
非常感谢。
【问题讨论】:
-
这取决于,我不知道您提到的这个库 Moa 是否默认保留内存缓存以及保留多少。您应该首先在其项目页面中了解这一点。尽管如此,我不知道您为什么使用库来下载图像,如果您确实尝试使用一个为您处理单元可重用性的库,例如 Kingfisher。
-
图像的尺寸是多少,单元格中图像视图的尺寸是多少?图像不应大于图像视图尺寸(当然是设备的
scale)。如果它们比这大,您可能需要调整图像大小以匹配。 -
仅供参考,图像所需的运行时内存通常为 4 x
widthxheight(这些是图像的尺寸,与图像视图的大小无关)。 -
我可以确定这些问题是由图像引起的。我已经在使用 alamofireImage 了,还是一样。
-
我可以在将它的图像放入我的 UIImageView 之前“调整”它的大小吗?我的 UIImageview 是 136x136 大小。
标签: ios swift uicollectionview