2016-10-03 7 views
0

私はAlamofireImageを使用しています。 保存したキャッシュイメージは実際には保存されません。AlamofireImage保存済みキャッシュイメージは実際には保存されません。

私のコードは次のとおりです。

import Alamofire 
import AlamofireImage 

let imageCache = AutoPurgingImageCache(
    memoryCapacity: 100 * 1024 * 1024, 
    preferredMemoryUsageAfterPurge: 60 * 1024 * 1024 
) 

が、私は自分が使用して画像を保存しています:

imageCache.add(image, withIdentifier: path) 

しかし、私が使用して、この画像を取得しようとすると、次の実行では、それは常にnilを返します。同じパス:

if let image = imageCache.image(withIdentifier: path){ 
    return image 
} 
return nil 

私は間違っていますか?

  • \デバッグを実行している間、私はそれのImageCache-currentMemoryUsage = 0

感謝を見ることができます。

答えて

0

add()メソッドを呼び出す必要はありません。 Alamofire Imageのキャッシュをラップするカスタムクラスを作成します。

class ImageManager: NSObject { 
    static let shared = ImageManager() 
    private var imageDownloader: ImageDownloader! 
    private var imageCache: AutoPurgingImageCache! 


    // Configuration 
    var maximumActiveDownloads = 5 
    var placeholderImage: UIImage! 
    var imageTransitionDuration: NSTimeInterval = 0.5 

    // The total memory capacity of the cache in MB. 
    var memoryCapacity: UInt64 = 150 

    //The preferred memory usage after purge in MB. During a purge, images will be purged until the memory capacity drops below this limit. 
    var memoryUsageAfterPurge: UInt64 = 60 


    private override init() { 
     super.init() 

     imageCache = AutoPurgingImageCache(
      memoryCapacity: memoryCapacity * 1024 * 1024, 
      preferredMemoryUsageAfterPurge: memoryUsageAfterPurge * 1024 * 1024 
     ) 

     imageDownloader = ImageDownloader(
      configuration: ImageDownloader.defaultURLSessionConfiguration(), 
      downloadPrioritization: .FIFO, 
      maximumActiveDownloads: maximumActiveDownloads, 
      imageCache: imageCache 
     ) 

     UIImageView.af_sharedImageDownloader = imageDownloader 
    } 


    func setImageOrPlaceholder(onImageView imageView: UIImageView, stringURL: String?) { 
     guard let correctURL = NSURL.getURL(fromString: stringURL) else { 
      //debug("URL incorrect!: \(stringURL)", isError: true) 

      imageView.image = placeholderImage 
      return 
     } 

     let imageTransition: UIImageView.ImageTransition = .CrossDissolve(imageTransitionDuration) 

     imageView.af_setImageWithURL(correctURL, 
            placeholderImage: placeholderImage, 
            filter: nil, 
            imageTransition: imageTransition, 
            completion: { response in 
             //   debug("\(response.result.value)") 
     }) 
    } } 
0

が、私は同じパスを使用して、この画像を取得しようとすると、次の実行では、それは常にnilを返します。

をあなたのアプリを再起動しているようこれが聞こえます。 ImageCacheはメモリのみで、アプリを再起動するとアプリのメモリが再利用されます。

関連する問題