照片上传到Firebase数据库和存储,但不将照片保存在应用程序中

时间:2019-11-21 04:23:26

标签: ios swift firebase firebase-storage

因此,我有一个编辑个人资料页面,用户可以在其中更改其个人资料图片和封面照片。我将其保存到用户选择图像的位置,然后单击saveButton并将照片上传到Firebase数据库和Firebase存储,然后关闭编辑配置文件viewcontroller并返回到配置文件视图。我唯一的问题是,尽管照片上传到了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)
        }
    }

1 个答案:

答案 0 :(得分:0)

正如我在函数中看到的那样 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
  }
}

我希望这会有所帮助:)