ios - 使用 firebase 检索图像给出 : EXC_BAD_ACCESS (code=1, 地址=0x10)

标签 ios swift firebase firebase-realtime-database

所以,我正在尝试缓存从 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 会加载图像,但下次出现时会发生这种情况:

  • 图片不会从缓存中加载
  • 我收到以下错误: Error

我注意到它在打印“No error”后崩溃了。

我想知道是什么原因导致了我的问题以及我该如何解决它。我认为这与 completionHandlers 有关,因为错误行暗示如此。

我也曾尝试在我的 completionHandler 中设置缓存,但没有任何反应。我做错了什么?

最佳答案

请检查fetcherCompletion是否为野指针,如添加NSLog(@"%@",weakSelf.fetcherCompletion)

如果这是真的,那可能是 Firebase 的错误。 fetcherCompletion 的属性应该添加copystrong

关于ios - 使用 firebase 检索图像给出 : EXC_BAD_ACCESS (code=1, 地址=0x10),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433615/

相关文章:

ios - 无法解析模块 react/lib/ReactUpdates

arrays - 快速将 PFObject 数组转换为自定义 PFSubclass

swift - iOS Today Extension 可以从应用程序的 Info.plist 中读取 URL 方案吗

php - Android 通过 firebase 推送通知(服务器端)

firebase - 如何正确更新事件流的 Firestore 查询快照?

ios - 如何在 iOS 7 的 UIPickerView 中更改文本字体?

ios - 为什么在发送任何 SSL 消息之前我的 SSL 握手失败?

javascript - Firebase 存在系统 : never stops writing

iOS 8 – 在设置关键窗口或关闭并立即呈现另一个 View Controller 后快速呈现 View Controller 时出现故障

swift - 通过 URL Scheme 打开 Cocoa App