如何将UIImagePickerController中选择的照片的URL快速分配给UILabel?

时间:2019-05-27 15:32:43

标签: swift xcode firebase firebase-storage

我试图以快速Xcode将所选图像的URL从UIImagePickerController分配给UILabel。图像数据将保存到Firebase实时数据库和Firebase存储中。但是,现在它不会将URL保存到数据库中,而是将图像保存到存储中。

我这里有一种方法可以从图片库中进行选择。

    // UIImagePickerControllerDelegate
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    guard let selectedImage = info[.originalImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

    // Set photoImageView to display selected image
    photoImageView.image = selectedImage

    // Dismiss picker
    dismiss(animated: true, completion: nil)

    let mediaType = info[UIImagePickerController.InfoKey.mediaType] as! NSString
    if mediaType.isEqual(to: kUTTypeImage as String) {
        let image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
        photoImageView.image = image

        if newPic == true {
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageError), nil)
        }
    }
}
// Cancel image picker
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
    dismiss(animated: true, completion: nil)
}

我在这里也有上传照片功能...

    func uploadPhoto(completion: @escaping (_ url: String?) -> Void) {

    let photoName = NSUUID().uuidString
    let storageRef = Storage.storage().reference().child("photoURL").child("\(photoName).jpg")

    let photo = self.photoImageView.image!

    if let uploadData = photo.jpegData(compressionQuality: 0.5) {
        storageRef.putData(uploadData, metadata: nil) { (metadata, error) in

            guard metadata != nil else {
                // Uh-oh, an error occurred!
                return
            }

            // You can also access to download URL after upload.
            storageRef.downloadURL { (url, error) in
                print(url!)

                guard url != nil else {
                    // Uh-oh, an error occurred!
                    return
                }


                let urlStr:String = (url?.absoluteString) ?? ""
                print(urlStr)

                print("Label before:\(String(describing: self.urlLabel.text))")
                self.urlLabel.text = "\(urlStr)"
                print("Label after:\(String(describing: self.urlLabel.text))")

            }
        }
    }
}

这种将其保存到数据库和存储中的方法...

func addPhoto() {
    self.refPhoto = Database.database().reference().child("photos");

    let key = self.refPhoto.childByAutoId().key
    let photo = [  "id" : key!, "photoURL" : self.urlLabel.text!] as [String: Any]

    self.refPhoto.child((Auth.auth().currentUser?.uid)!).child(key!).updateChildValues(photo)

    uploadPhoto() { url in
        self.urlLabel.text = url
        let url = ["photoURL" : self.urlLabel.text!]
        self.refPhoto.child((Auth.auth().currentUser?.uid)!).updateChildValues(url)
    }
}

0 个答案:

没有答案