UIView
我使用Kingfisher将图像加载到卡片视图(while 再次变为 39,但问题是当内存被释放时,在应用程序中第二个 UI 变慢(我认为这被主线程阻塞了)。这个问题和这个类似。我该如何解决?
我在中设置了翠鸟的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 兆字节的图像时,会发生这种内存跳跃。我发现在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
}
}