Learn Courses My Dashboard

Firebase Storage Reference Error

Hi all - I am trying to follow Chris’s process for uploading photos to Firebase (reference link: Uploading Images to Firebase Storage (and retrieving them) - YouTube).

I am able to go through the process of selecting an image from the simulator and it appears in the UIView in the simulator, but I get the following error when I try to post it to Firebase: “Thread 1: EXC_BAD_ACCESS (code=257, address=0x1f)”.

This error appears next to the storage.reference() code, but also appears in the “Thread 1” dropdown in the Debug Navigator.

I did a bit of research and it appears to be an error related to memory (EXC_BAD_ACCESS crash error: Understanding and solving it - SwiftLee). I’ve also tried making the Firebase permissions public, but that gives the same error, so I think we can rule this out as the problem.

Has anyone come across this before and can share troubleshooting solutions?

See photo of the error to reference:

Error next to storage.reference code:

See code below:

import Photos
import PhotosUI
import UIKit
import Firebase
import FirebaseStorage

class AddPostController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
        
    @IBOutlet weak var postImageView: UIImageView!
    @IBOutlet weak var uploadPostButton: UIBarButtonItem!
    @IBOutlet weak var commentText: UITextField!
    @IBOutlet weak var chooseImageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        chooseImageView.isUserInteractionEnabled = true
        let gestureRecognizer = UITapGestureRecognizer(target: self , action: #selector(chooseImage))
        chooseImageView.addGestureRecognizer(gestureRecognizer)
    }
    
    @objc func chooseImage() {
        let pickerController = UIImagePickerController()
        pickerController.delegate = self
        pickerController.sourceType = .photoLibrary
        present(pickerController, animated: true, completion: nil)
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        postImageView.image = info[.originalImage] as? UIImage
        self.dismiss(animated: true, completion: nil)
    }
    
    @IBAction func uploadPostButtonClicked(_ sender: Any) {
        let storage = Storage.storage()
        let storageReference = storage.reference()

        let mediaFolder = storageReference.child("media")

        if let data = postImageView.image?.jpegData(compressionQuality: 0.1) {

            let imageReference = mediaFolder.child("image.jpeg")
            imageReference.putData(data, metadata: nil) { (metadata, error) in
                if error != nil {
                    print(error?.localizedDescription)
                } else {
                    imageReference.downloadURL { (url, error)  in
                        if error == nil {
                            let imageUrl = url?.absoluteString
                            print(imageUrl)
                        }
                    }
                }
            }
        }
    }
}

Welcome to the community!

You‘re showing UIKit code but the video you linked is in SwiftUI?

Are you trying to follow the SwiftUI tutorial but all your code is still in UIKit?

Double check your IBAction is connected to your storyboard properly

comment everything out and just print something so when you tap the button it prints (or you could look at the storyboard file itself)