我正在使用Firebase托管和下载图像为我的应用程序。Firebase上的每一张图片都在200 as 400 as之间,用户在浏览collectionView时一次下载大约12次。当我启动下载图像的VC时,我的应用程序从使用大约100 my的内存到下载19张图像的650 my内存。问题中的图片是JPEG,在上传到Firebase之前,它们已经被压缩得很重。这些映像存储在NSCache中,清除缓存会使内存使用量降到大约100 an。
怎么一回事?以下是一些可能有用的代码:
类TripOverviewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
func updateUI(photo:Photo, image:UIImage? = nil) {
//Call when preparing to show image
if image != nil {
print("Loaded from cache")
imageView.image = image
photo.assignImage(image: image!)
} else {
let url = photo.imageUrl
let ref = FIRStorage.storage().reference(forURL: url)
ref.data(withMaxSize: 5*1024*1024, completion: { [weak self] (data, error) in
if error != nil {
print("Unable to download image")
} else {
print("Image downloaded")
if let imageData = data {
if let image = UIImage(data: imageData) {
self?.imageView.image = image
photo.assignImage(image: image)
TripsVC.imageCache.setObject(image, forKey: photo.uid as NSString)
}
}
}
})
}
}
}发布于 2017-03-19 00:50:25
正如Frank评论的那样,显示的图像所占用的空间比图像的数据要大得多。我的错误在于我在缓存UIImages,这是一个完整显示的图像。相反,我应该缓存数据,然后从需要它们的数据中创建图像。内存使用量从550 to下降到大约20 to。
https://stackoverflow.com/questions/42864348
复制相似问题