为什么我的Firebase Storage照片没有加载到UICollectionView中?

时间:2019-10-10 20:47:24

标签: ios swift firebase uicollectionview firebase-storage

我正在iOS应用中使用UICollectionView,以允许用户以网格视图查看他们的照片。他们的照片存储在Firebase存储中,应将其提取并显示在UICollectionView中。但是,我不确定该怎么做。我在互联网上进行搜索,但找不到有关如何成功完成此操作的任何优质教程。我还搜索了堆栈溢出,除了this one之外找不到任何问题,但是它没有答案。这是我当前代码的演练:

此功能用于将图像上传到Firebase存储。 uploadImage(imageData: )接受通过Data检索的UIImagePickerController对象。此功能运行良好:

func uploadImage(imageData: Data) {
    let imageID = UUID().uuidString
    let storageRef = Storage.storage().reference(withPath: "\(partyID)_photo_sharing_images/\(imageID).jpg")
    let metadata = StorageMetadata()
    metadata.contentType = "image/jpeg"

    storageRef.putData(imageData, metadata: metadata) { (metadata, err) in
        if let err = err {
           self.showAlertWithOK(title: "An Error Occurred", message: "Something went wrong while uploading your image.")
               print("An error occurred: \(err)")
           }

           storageRef.downloadURL { (url, err) in
              if let err = err {
                  print("An error occurred: \(err.localizedDescription)")
              }

              guard let url = url else { return }
              self.uploadImageData(url: url.absoluteString)
          }
     }
}

将图像上传到Firebase存储后,我将使用功能uploadImageData(url: )将图像URL上传到Google Cloud Firestore。在那之后,我开始为代码苦苦挣扎。

我创建了一个名为Photo的自定义对象,该对象具有三个不同的值:URLStringInt。我有一个Photo对象的数组,我从数组中读取了图像,并将这些图像从Firebase存储下载到UICollectionViewCell中。这是执行此操作的代码:

func loadImagesData() {
    let db = Firestore.firestore()
    let partyRef = db.collection("parties").document(partyID)

    partyRef.addSnapshotListener() { (snapshot, err) in
        if let err = err {
           print("An error occurred: \(err.localizedDescription)")
           self.showAlertWithOK(title: "An Error Occurred", message: err.localizedDescription)
        } else {
           let photoSharingImages = snapshot?.data()?["photo_sharing_images"] as? [String] ?? [String]()

           for photoSharingImage in photoSharingImages {
               guard let url = URL(string: photoSharingImage) else {
               print("Unable to retrieve URL from photo sharing image URL.")
               return
           }

            let photo = Photo(photographer: "none", likes: 0, url: url)
            self.photos.append(photo)
         }

         print("These are the URL from the array: \(photoSharingImages)")
     } 
}

loadImagesData()从Firestore获取图像下载URL,并将其添加到Photo中。然后将新的Photo附加到photos数组中。 接下来,需要使用从Firestore获取的下载URL将photos添加到UICollectionView。我当前正在使用一个名为loadImage()的函数,该函数从存储中加载图像并将其加载到内存中。这是我为此创建的功能。

func loadImage(photo: Photo) -> UIImage {
    let storageRef = Storage.storage().reference(forURL: photo.url.absoluteString)

    var image: UIImage?
    storageRef.getData(maxSize: 1 * 1024 * 1024) { data, err in
       if let err = err {
         print("An error occurred: \(err.localizedDescription)")
       } else {
         if let data = data {
           image = UIImage(data: data)
         }
       }
    }

    return image ?? UIImage()
}

然后我像这样在collectionView(_ collectionView:, cellForItemAt indexPath: )中调用该函数:

let photo = photos[indexPath.row]

// Configure the cell.
cell.photoImageView.image = loadImage(photo: photo)

但是,不幸的是,这些照片并未显示在UICollectionViewCell的{​​{1}}中。为什么会这样呢?如何将照片正确加载到UIImageView的{​​{1}}中以解决此问题。

0 个答案:

没有答案