Firebase和Swift 4.2上传图像并将其下载到表格视图

时间:2018-10-02 12:53:15

标签: ios swift firebase firebase-storage

尝试上传并下载到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
                    }
                }
            }
        })
    }

}

}

0 个答案:

没有答案