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

问题描述 投票:1回答:1

因此,我有一个编辑个人资料页面,用户可以在其中更改其个人资料图片和封面照片。我将其保存到用户选择图像的位置,然后单击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)
        }
    }
ios swift firebase firebase-storage
1个回答
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)
    }
}

我希望这会有所帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.