Firebase и Swift 4.2 загрузка изображения и загрузка его в виде таблицы - PullRequest
0 голосов
/ 02 октября 2018

Попытка выгрузки и загрузки в базу данных Firebase, однако я получаю ошибку Завершение работы приложения из-за необработанного исключения NSInvalidArgumentException

причина: 'Предоставлено ведро: memestreamios.appspot.comMemes не соответствует корзине хранилища текущего экземпляра: memestreamios.appspot.com

Я пробовал несколько вещей, но не могу заставить его работать.

// Это код почтовой страницы

class Post {

// Variables
var caption : String!
var imageDownloadURL : String!
private var image : UIImage!

// Iniotialization of the variables
init(image : UIImage, caption : String) {
    self.caption = caption
    self.image = image
}

// Snapshot the bdata
init(snapshot : DataSnapshot) {
    let json = JSON(snapshot.value ?? "")
    self.caption = json["caption"].stringValue
    self.imageDownloadURL = json["imageDownloadURL"].stringValue
}

// Function to save the newPost
func save() {

    // 1 - Create a new Database Reference
    let newPostRef = Database.database().reference().child("posts").childByAutoId()
    let newPostKey = newPostRef.key

    // Convert image into data
    if let imageData = self.image.jpegData(compressionQuality: 0.6) {

        // 2 - Create a new Strage Reference
        let imageStorageRef = Storage.storage().reference().child("Memes")
        let newImageRef = imageStorageRef.child(newPostKey!)

        // 3 - Save the image to the storage URL
        newImageRef.putData(imageData).observe(.success, handler: { (snapshot) in

            let smallPath = snapshot.reference.fullPath

            self.imageDownloadURL = ("gs://memestreamios.appspot.com/\(smallPath)")

            let newDictionary = [
                "imageDownloadURL" : self.imageDownloadURL,
                "caption" : self.caption
            ]

            newPostRef.setValue(newDictionary)
        })

    }


}

}

// Это код страницы просмотра таблицы, где я хочу, чтобы изображения отображались

class PhotoTableViewCell: UITableViewCell {

// variables
var post : Post! {
    didSet {
        self.updateUI()
    }
}


@IBOutlet weak var postImageView: UIImageView!
@IBOutlet weak var captionLabel: UILabel!
@IBOutlet weak var shadowBackgroundView: UIView!

func updateUI() {
    // Set shadow background view
    shadowBackgroundView.layer.shadowPath = UIBezierPath(rect: shadowBackgroundView.bounds).cgPath
    shadowBackgroundView.layer.shadowColor = UIColor.black.cgColor
    shadowBackgroundView.layer.shadowOpacity = 0.1
    shadowBackgroundView.layer.shadowOffset = CGSize(width: 2, height: 2)
    shadowBackgroundView.layer.shadowRadius = 2
    shadowBackgroundView.layer.masksToBounds = false
    shadowBackgroundView.layer.cornerRadius = 3.0

    // Caption
    self.captionLabel.text = post.caption

    // Download the post photo from Firebase
    if let image = post.imageDownloadURL{

        // 1 - Create the Storage Reference
        let imageStorageRef = Storage.storage().reference(forURL: image)


        // 2 - Observe method to download the data
        imageStorageRef.getData(maxSize: 2 * 1024 * 1024, completion: { [weak self](data, error) in
            if let error = error {
                print("*** ERROR DOWNLOAD IMAGE : \(error)")
            } else {
                // Success
                if let imageData = data {
                    // 3 - Put the image in imageview
                    DispatchQueue.main.async {
                        let image = UIImage(data: imageData)
                        self?.postImageView.image = image
                    }
                }
            }
        })
    }

}

}
...