Swift - 上传/从Firebase检索图像

时间:2017-12-27 13:11:22

标签: ios swift firebase firebase-realtime-database firebase-storage

我希望我的用户能够在我的应用程序中设置他们的个人资料图片。我持有包含用户的个人资料图片的个人资料数据作为表格视图单元格。但是,我的代码中存在问题。

这是我的表格视图功能:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if(indexPath.section == 0) {
        let profileInfoCell = tableView.dequeueReusableCell(withIdentifier: "MyProfileInfoCell") as! MyProfileInfoCell

        profileInfoCell.profilePicture.tag = indexPath.row

        let user = users[indexPath.row]

        profileInfoCell.username.text = user.username
        profileInfoCell.userEmail.text = user.email
        profileInfoCell.userReputation.text = "has 2 answered likes"

        /*
        let profilePictureStorageRef = Storage.storage().reference().child("profilePicture").child((Auth.auth().currentUser?.uid)!)
        profilePictureStorageRef.getData(maxSize: 1 * 1024 * 1024, completion: {
            data, error in
            if let error = error {

            } else {
                let image = UIImage(data: data!)
                profileInfoCell.profilePicture.image = image!
            }
        })
        */

        return profileInfoCell
    }

这是我的imagePickerController函数:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let indexPath = NSIndexPath(row: 0, section: 0)
    let cell = tableView.cellForRow(at: indexPath as IndexPath) as! MyProfileInfoCell!

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        selectedImage = image
        cell?.profilePicture.contentMode = .scaleAspectFit
        cell?.profilePicture.image = image
    }

    /*
    let profilePictureStorageRef = Storage.storage().reference(forURL: "gs://cs401-402.appspot.com").child("profilePicture").child((Auth.auth().currentUser?.uid)!)

    if let profilePicture = self.selectedImage, let imageData = UIImagePNGRepresentation((cell?.profilePicture.image!)!) {
        profilePictureStorageRef.putData(imageData, metadata: nil, completion: {
            (metadata, error) in
            if error != nil {
                return
            }

            let profilePictureUrl = metadata?.downloadURL()?.absoluteString

            self.ref.child("users").child((Auth.auth().currentUser?.uid)!).child("profilePicture").setValue(profilePictureUrl!)
        })
    }
    */

    self.dismiss(animated: true, completion: nil)
}

我的程序正确处理设置个人资料图片。但是,当我在选择个人资料图片后尝试使用Firebase时,我遇到了问题。 imagePickerController函数中的注释行导致我的表格视图单元格与配置文件信息的多次创建。

如果有人能帮助我,我会很感激。感谢。

0 个答案:

没有答案