首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AlamofireImage如何清理内存和缓存

AlamofireImage如何清理内存和缓存
EN

Stack Overflow用户
提问于 2016-10-10 11:36:18
回答 1查看 1.3K关注 0票数 1

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

代码语言:javascript
复制
        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模拟器中捕捉到它,但我检查了一下,我认为手机用户的内存是否会崩溃。我用破折号来捕捉撞车,但我在破折号中看不到这个碰撞。我检查了我的Nginx服务器中的日志(访问和错误),也没有发现任何问题和错误。

在那之后,我想电话里的记忆出了问题。如果我使用Alamofire,我如何清理缓存和内存?我能清理一下阿拉莫火吗?

在photo_url请求之前,我尝试使用这个函数并在缓存中清理图像,但我也遇到了相同的崩溃:

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2018-05-02 11:11:17

更新为“迅速”:

简单函数下面使用的

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39957363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档