Swift AlamofireImage如何清理内存和缓存
我在应用程序中使用Alamofire和AlamofireImage获取数据和照片Swift AlamofireImage如何清理内存和缓存,swift,memory,alamofire,alamofireimage,Swift,Memory,Alamofire,Alamofireimage,我在应用程序中使用Alamofire和AlamofireImage获取数据和照片 Alamofire.request(.GET, URLString, encoding: .JSON) .authenticate(user: user, password: password) .responseJSON() { (response) -> Void in if let jsonResult = response.resul
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服务器中的日志(访问和错误),也没有发现任何问题和错误
之后,我认为这是手机内存的问题。如果我使用Alamofire,如何清理缓存和内存?我能清理一下阿拉莫菲尔吗
在我的photo_url请求之前,我尝试使用此函数清理缓存中的图像,但我遇到了相同的崩溃:
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)
}
为swift更新:
用于以下简单函数强>
//注意:-其中占位符=您的\u占位符\u图像\u名称
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
})
}