2017-06-06 69 views
0

我使用Kingfisher將圖像加載到卡片視圖(UIView)中,就像在啓動時一樣,我將Kingfisher設置爲將圖像緩存到磁盤,但在卡片分頁過程中,內存消耗增加顯着從39兆字節到247,並在一段時間後再次成爲39,但問題是,當內存被釋放,在第二個用戶界面的應用程序變慢(我認爲這是由主線程阻止)。這個問題類似於this one.我該如何解決它?Kingfisher中的奇怪RAM行爲

didFinishLaunchingWithOptions

fileprivate func setupKingfisherSettings() { 
     let megabytes: UInt = 300 
     ImageCache.default.maxDiskCacheSize = megabytes * 1024 * 1024 
     ImageCache.default.maxMemoryCost = 1 
    } 

代碼片段設置設置Kingfisher的AppDelegate。當我刪除這段代碼時,這個問題沒有發生。

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") 
    } 
} 

enter image description here

更新時間::我發現了一個規律,當這種情況發生。當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 
     } 
    } 
+0

請提供代碼片段,我也遇到與翠鳥相同的內存問題,並在翠鳥發佈問題。似乎有一些問題需要修復,最後我切換到SDWebImage。 – dip

+0

@dip另外我想說的是,我在翠鳥問題中發表了這個問題 – Alexander

+0

@dip我的問題https://github.com/onevcat/Kingfisher/issues/703 – Alexander

回答

0

目前我解決了我的問題,我寫了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) 
    } 

}