swift 照片上传至Firebase数据库和存储,但未在应用程序中保存照片

eyh26e7m  于 2022-12-10  发布在  Swift
关注(0)|答案(1)|浏览(146)

所以我有一个编辑个人资料页面,用户可以在其中更改他们的个人资料图片和封面照片。我有它的地方,一旦用户选择他们的图像,他们点击保存按钮,照片被上传到Firebase数据库和Firebase存储,然后编辑个人资料视图控制器被解除,返回到个人资料视图。唯一的问题是,我有的是,虽然照片上传到Firebase,如果我关闭并重新打开应用程序,它不会将图片保存在应用程序上。我不知道我错过了什么,使之发生。下面是代码:

import UIKit
import Foundation
import Firebase
import FirebaseDatabase

class NewEditProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!

    var ref = DatabaseReference.init()

    var imagePicker = UIImagePickerController()
    var imagePicked = 0

    var selectedImage1: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.ref = Database.database().reference()
        self.saveFIRData()
        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = true

    }

    @IBAction func chooseImage1(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
            present(imagePicker, animated: true)
        }

        }

    @IBAction func chooseImage2(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
                present(imagePicker, animated: true)
    }

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
        let pickedImage2 = info[UIImagePickerController.InfoKey.editedImage] as? UIImage

        if imagePicked == 1 {
            self.imageView1.image = pickedImage
        } else if imagePicked == 2 {
            self.imageView2.image = pickedImage2
        }
        dismiss(animated: true)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }

@IBAction func saveButton(_ sender: Any) {
    self.saveFIRData()
    self.dismiss(animated: true, completion: nil)
    }

    @IBAction func backButton(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
        }
    @objc func saveFIRData() {
        guard let image = imageView1.image else { return }

        self.uploadProfileImage(image){ url in

            self.saveProfileImage(profileURL: url!){ success in
                if success != nil{
                    print("yes")
                }
            }
        }
    }
}

extension NewEditProfileViewController {

    func uploadProfileImage(_ image:UIImage, completion: @escaping (_ url: URL?)->()) {
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let storageRef = Storage.storage().reference().child("users/\(uid)")
        let imageData = imageView1.image?.jpegData(compressionQuality: 0.8)
        let metaData = StorageMetadata()
        metaData.contentType = "image/jpeg"
        storageRef.putData(imageData!, metadata: metaData) { (metaData, error) in
            if error == nil{
                print("success")
                storageRef.downloadURL(completion: { (url, error) in
                    completion(url)
                })
            }else{
                print("error in save image")
                completion(nil)
            }
        }
    }

    func saveProfileImage(profileURL:URL, completion: @escaping ((_ url: URL?) -> ())){
        guard let uid = Auth.auth().currentUser?.uid else { return }
        let databaseRef = Database.database().reference().child("users/profilePhotoURL/\(uid)")
        let userObject = [
            "photoURL": profileURL.absoluteString
        ] as [String:Any]

        self.ref.child("users").child(uid).setValue(userObject)
        }
    }
qhhrdooz

qhhrdooz1#

正如我可以看到在您的代码中的函数func saveProfileImage(profileURL:URL, completion: @escaping ((_ url: URL?) -> ()))代码下载图像是失踪,你必须更新你的saveProfileImage,以支持下载&然后保存到本地应用程序目录。

func saveProfileImage(profileURL:URL, completion: @escaping ((_ url: URL?) -> ())){
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let databaseRef = Storage.storage().reference().child("users/profilePhotoURL/\(uid)")

    // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes), Change it according to your need
    storageRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
      if let error = error {
        // Uh-oh, an error occurred!
      } else {
        // Save `data` to your local directory here
        let imageFilename = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "/" + "\(uid).jpg"
        data.write(toFile: imageFilename, atomically: true) 
      }
    }

    let userObject = [
        "photoURL": profileURL.absoluteString
    ] as [String:Any]

    self.ref.child("users").child(uid).setValue(userObject)
    }
}

请参阅以下示例代码,这些代码摘自文件“在iOS上下载文件”页面上的Firebase指南Download to local部分。

// Create a reference to the file you want to download
let islandRef = storageRef.child("images/island.jpg")

// Create local filesystem URL
let localURL = URL(string: "path/to/image")!

// Download to the local filesystem
let downloadTask = islandRef.write(toFile: localURL) { url, error in
  if let error = error {
    // Uh-oh, an error occurred!
  } else {
    // Local file URL for "images/island.jpg" is returned
  }
}

相关问题