AlamofireImage如何清理内存和缓存

编程入门 行业动态 更新时间:2024-10-22 14:42:33
本文介绍了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如何清理内存和缓存

本文发布于:2023-11-26 02:59:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632364.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   内存   AlamofireImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!