所以,我正在尝试缓存从 firebase 检索到的图像:
class AccountSettings: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{
@IBOutlet weak var profileImage: UIImageView!
var imageRef: StorageReference{
return Storage.storage().reference().child("profile_images")
}
let cache = NSCache<NSString, UIImage>()
var activityIndicator = UIActivityIndicatorView()
override func viewDidLoad() {
activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)
activityIndicator.hidesWhenStopped = true
profileImage.addSubview(activityIndicator)
activityIndicator.startAnimating()
if let image = cache.object(forKey: "\(UserDefaults.standard.string(forKey: "unique_id")!).jpg" as NSString){
profileImage.image = image
}else{
retrieveCurrentProfileImg()
}
}
func retrieveCurrentProfileImg(){
let downloadImageRef = imageRef.child("\(UserDefaults.standard.string(forKey: "unique_id")!).jpg")
let downloadTask = downloadImageRef.getData(maxSize: 1024*1024*12) { (data, error) in
if let data = data{
let image = UIImage(data: data)
self.profileImage.image = image
self.cache.setObject(image!, forKey: "\(UserDefaults.standard.string(forKey: "unique_id")!).jpg" as NSString)
self.activityIndicator.stopAnimating()
}
print(error ?? "No error")
}
downloadTask.observe(.progress) { (snapshot) in
}
downloadTask.resume()
}
第一次出现时, View 会加载图像,但下次出现时会发生这种情况:
我注意到它在打印“No error”后崩溃了。
我想知道是什么原因导致了我的问题以及我该如何解决它。我认为这与 completionHandlers 有关,因为错误行暗示如此。
我也曾尝试在我的 completionHandler 中设置缓存,但没有任何反应。我做错了什么?
最佳答案
请检查fetcherCompletion是否为野指针,如添加NSLog(@"%@",weakSelf.fetcherCompletion)
如果这是真的,那可能是 Firebase 的错误。 fetcherCompletion 的属性应该添加copy
或strong
。
关于ios - 使用 firebase 检索图像给出 : EXC_BAD_ACCESS (code=1, 地址=0x10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433615/