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

时间:2016-11-22 06:06:18

标签: ios swift firebase upload firebase-storage

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

1 个答案:

答案 0 :(得分:0)

  • 您必须将图片上传到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/

  • 中找到完整的参考资料