AlamofireImage如何清理内存和缓存 [英] AlamofireImage how to clean memory and cache

查看:212
本文介绍了AlamofireImage如何清理内存和缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用Alamofire和AlamofireImage来获取数据和照片.

I use Alamofire and AlamofireImage in my app for get data and photos.

        Alamofire.request(.GET, URLString, encoding: .JSON)
        .authenticate(user: user, password: password)
        .responseJSON() { (response) -> Void in
          if let jsonResult = response.result.value {
            if jsonResult.count > 0 {
               ...
               let photo_url = jsonResult[index]["Photo_url"] as! String
               Alamofire.request(.GET, photo_url)
               .authenticate(user: user_photo, password: password_photo)
               .responseImage { response in
                  if let image = response.result.value
                  {
                     button.setBackgroundImage(image, forState: .Normal)
                  }
               }
            }
          }
        }

如果用户长时间使用应用程序,则会崩溃.我无法在Mac上的IOS模拟器中捕获它,但是我检查了一下,我认为电话完成用户中的内存是否崩溃.我使用Crashlytics进行捕获崩溃,但是在Crashlytics中看不到此崩溃. 我在Nginx服务器中检查了日志(访问和错误),也没有看到任何问题和错误.

I have a crash if user use app for a long time. I can't caught it in the IOS simulator on mac, but I checked and I think if memory in phone finished user have a crash. I use Crashlytics for catch crash but I can't see this crash in the Crashlytics. I checked logs (access and error) in my Nginx server and also din't see any problems and errors.

在那之后,我认为这是手机内存的问题.如果使用Alamofire,如何清除缓存和内存?我可以清理Alamofire缓存吗?

After there I think it is problem with memory in the phone. How can I clean cache and memory if I use Alamofire? Can I clean cache Alamofire?

在我提出photo_url请求之前,我尝试使用此功能并清除缓存中的图像,但是我遇到了相同的崩溃:

I tried to use this function and clean image in the cache before my photo_url request, but I have the same crash:

func clearImageFromCache(url: String) {
    let URL = NSURL(string: url)!
    let URLRequest = NSURLRequest(URL: URL)

    let imageDownloader = UIImageView.af_sharedImageDownloader

    // Clear the URLRequest from the in-memory cache
    imageDownloader.imageCache?.removeImageForRequest(URLRequest, withAdditionalIdentifier: nil)

    // Clear the URLRequest from the on-disk cache
    imageDownloader.sessionManager.session.configuration.URLCache?.removeCachedResponseForRequest(URLRequest)
}

推荐答案

更新迅速:

Updated for swift:

在下面的简单功能中使用;

Used below simple function;

func clearImageFromCache(mYourImageURL: String) {
    let URL = NSURL(string: mYourImageURL)!
    let mURLRequest = NSURLRequest(url: URL as URL)
    let urlRequest = URLRequest(url: URL as URL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData)

    let imageDownloader = UIImageView.af_sharedImageDownloader
    _ = imageDownloader.imageCache?.removeImage(for: mURLRequest as URLRequest, withIdentifier: nil)
    self.mImageView.af_setImage(withURLRequest: urlRequest, placeholderImage: UIImage(named: "placeholder"), completion: { (response) in
        self.mImageView.image = response.result.value
    })
}

//注意:-其中占位符= your_placeholder_image_name

// NOTE:- where placeholder = your_placeholder_image_name

这篇关于AlamofireImage如何清理内存和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆