我使用Kingfisher将图像加载到Card View(UIView
)中,就像在火种中一样,我将Kingfisher设置为将图像缓存到磁盘,但是在分页卡期间,内存消耗从39 MB急剧增加到247,然后一会儿又变成了39,但是问题是当释放内存时,在应用程序中的第二个UI变慢了(我认为这被主线程阻止了)。此问题类似于this one.如何解决?
我在AppDelegate
中的didFinishLaunchingWithOptions
中设置了翠鸟的设置
fileprivate func setupKingfisherSettings() {
let megabytes: UInt = 300
ImageCache.default.maxDiskCacheSize = megabytes * 1024 * 1024
ImageCache.default.maxMemoryCost = 1
}
代码段。当我删除此代码时,不会发生此问题。
private func downloadImages(_ card: CardModel) {
if let placeAvatarURLString = card.photoURLsProperties.placePhotoURLs.first {
if let placeAvatarURL = URL(string: placeAvatarURLString) {
venueImageView.kf.indicatorType = .activity
venueImageView.kf.setImage(with: placeAvatarURL)
} else {
venueImageView.image = UIImage(named: "CardDefaultImage")
}
} else if let eventLogoURLPath = card.photoURLsProperties.placeLogoURLs.first {
if let url = URL(string: eventLogoURLPath) {
venueImageView.kf.indicatorType = .activity
venueImageView.kf.setImage(with: url)
} else {
venueImageView.image = UIImage(named: "CardDefaultImage")
}
} else {
venueImageView.image = UIImage(named: "CardDefaultImage")
}
}
更新::这种情况发生时我发现了规律性。当ImageCache提取大于5 MB的图像时,就会发生内存跳跃。我在该方法diskImage中使用debugPrint发现它,如果图像等于或大于5兆字节,则存在一个跳跃,如果4兆字节,则一切都很好。
我正在iPhone 7上进行测试,超过74 GB的可用内存。
func diskImage(forComputedKey key: String, serializer: CacheSerializer, options: KingfisherOptionsInfo) -> Image? {
if let data = diskImageData(forComputedKey: key) {
debugPrint("ImageCache data.count", data.count / 1024 / 1024)
return serializer.image(with: data, options: options)
} else {
return nil
}
}
最佳答案
在解决问题的那一刻,我编写了Custom CacheSerializer,并将图像压缩到3 MB。但是我对其他答案感兴趣,如何解决这个问题。
import Kingfisher
struct AppNameKingfisherCacheSerializer: CacheSerializer {
func data(with image: Image, original: Data?) -> Data? {
return image.compressToData(3)
}
func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
return UIImage(data: data)
}
}
关于ios - 翠鸟中奇怪的RAM行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44381191/