如何通过 Firebase 从我的 iOS 应用程序上传和检索图像?

How can I upload and retrieve images from my iOS application through Firebase?

我想将图像从我的一个视图控制器上传到 Firebase,然后我想将上传的图像加载到另一个视图控制器。我也希望这个过程对无限用户继续下去。我不知道该怎么做。

  • 您必须按照下面的说明将图片上传到 firebase

    // Data in memory
    let data: NSData = ...
    
    // Create a reference to the file you want to upload
    let riversRef = storageRef.child("images/rivers.jpg")
    
    // Upload the file to the path "images/rivers.jpg"
    let uploadTask = riversRef.putData(data, metadata: nil) { metadata, error in
    if (error != nil) {
    // Uh-oh, an error occurred!
    } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    let downloadURL = metadata!.downloadURL
    }
    }
    
  • 获取图片后如下图

    // Create a reference to the file you want to download
    let islandRef = storageRef.child("images/island.jpg")
    
    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
    islandRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void           in
    if (error != nil) {
    // Uh-oh, an error occurred!
    } else {
    // Data for "images/island.jpg" is returned
    // ... let islandImage: UIImage! = UIImage(data: data!)
    }
    }
    
  • 您还可以在 https://firebase.google.com/docs/storage/ios/

  • 中找到完整的参考资料