将多个图像另存为字符串

时间:2019-06-17 02:39:29

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

我一直在尝试其他方法来将图像按钮保存到Firebase。但是,我尝试的每种方式都会遇到不同的错误。我想知道是否有人可以看一下我的代码并可能将我引向正确的方向。

下面是我的@objc func handlePost()。单击此按钮后,我希望用户能够将数据保存到firebase数据库中。我希望将用户的照片保存到存储中,然后将URL保存到数据库中。

@objc func handlePost() {

    let timestamp = Int(Date().timeIntervalSince1970)
    guard let category = JobCategory.text, let description = JobBio.text, let cost = price.text else {
        print("Error")
        return
    }

    guard let uid = Auth.auth().currentUser?.uid else { return }
    self.mapView?.isMyLocationEnabled = true
    let cords = ["Latitude": self.locationManager.location!.coordinate.latitude, "Longitude": self.locationManager.location!.coordinate.longitude]
    let docData: [String: Any] = [
        "cords" : cords,
        "uid": uid,
        "category": category,
        "imageUrl1": poster?.imageUrl1 ?? "",
        "imageUrl2": poster?.imageUrl2 ?? "",
        "imageUrl3": poster?.imageUrl3 ?? "",
        "price": cost,
        "description": description,
        "timeStamp": timestamp
    ]

    let hud = JGProgressHUD(style: .dark)
    hud.textLabel.text = "Posting Job"
    hud.show(in: view)
    self.postJobDataIntoDatabseWithUID(uid: uid, values: docData as [String : AnyObject])

    print("Finished saving user info")
    self.dismiss(animated: true, completion: {
        print("Dismissal complete")
    })
    hud.dismiss()
}
private func postJobDataIntoDatabseWithUID(uid: String, values: [String: AnyObject]) {
    let ref = Database.database().reference(fromURL: "https://oddjobs-b131f.firebaseio.com/")
    let usersReference = ref.child("JobPost").child(uid)
    usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
        if err != nil {
            print("err")
            return
        }

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

}

最后是我的扩展程序文件,该文件控制将照片上传到Firebase存储。

 lazy var image1Button = createButton(selector: #selector(handleSelectPhoto))
lazy var image2Button = createButton(selector: #selector(handleSelectPhoto))
lazy var image3Button = createButton(selector: #selector(handleSelectPhoto))

@objc func handleSelectPhoto(button: UIButton) {
    print("Select photo with button:", button)
    let imagePicker = CustomImagePickerController()
    imagePicker.delegate = self
    imagePicker.imageButton = button
    present(imagePicker, animated: true)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    let selectedImage = info[.originalImage] as? UIImage
    let imageButton = (picker as? CustomImagePickerController)?.imageButton
    imageButton?.setImage(selectedImage?.withRenderingMode(.alwaysOriginal), for: .normal)
    dismiss(animated: true)

    let filename = UUID().uuidString
    let ref = Storage.storage().reference().child("job_images").child("\(filename).png")
    guard let uploadData = selectedImage?.jpegData(compressionQuality: 0.75) else { return }

    let hud = JGProgressHUD(style: .dark)
    hud.textLabel.text = "Uploading image..."
    hud.show(in: view)
    ref.putData(uploadData, metadata: nil) { (nil, err) in
        if let err = err {
            hud.dismiss()
            print("Failed to upload image to storage:", err)
            return
        }

        print("Finished uploading image")
        ref.downloadURL(completion: { (url, err) in

            hud.dismiss()

            if let err = err {
                print("Failed to retrieve download URL:", err)
                return
            }

            print("Finished getting download url:", url?.absoluteString ?? "")

            if imageButton == self.image1Button {
                self.poster?.imageUrl1 = url?.absoluteString
            } else if imageButton == self.image2Button {
                self.poster?.imageUrl2 = url?.absoluteString
            } else {
                self.poster?.imageUrl3 = url?.absoluteString
            }


        })
    }
}

我认为这将允许我将三张照片保存到Firebase数据库中,但是从存储中检索URL并将其发布到数据库中似乎出现了问题。如果有人可以帮助我,我将不胜感激!

0 个答案:

没有答案