一切都很完美。我看到一张图片上传到我的Firebase存储空间,唯一的问题是,它是我作为图片视图放置的图片,而不是从相机胶卷中选择的图片。
import UIKit
import Firebase
class ProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var profilePicture: UIImageView!
@IBAction func dismissButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
profilePicture.translatesAutoresizingMaskIntoConstraints = false
profilePicture.addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(handleSelectProfileImageView)))
guard let uid = FIRAuth.auth()?.currentUser?.uid else {
return
}
let imageName = NSUUID().uuidString
let storageRef = FIRStorage.storage().reference().child("Profile_Images").child("\(imageName).jpg")
if let uploadData = UIImageJPEGRepresentation(self.profilePicture.image!, 0.1){
storageRef.put(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil{
print(error as Any)
return
}
if let profileImageURL = metadata?.downloadURL()?.absoluteString {
let values = ["profileImageUrl": profileImageURL];
self.registerUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
}
})
}
}
private func registerUserIntoDatabaseWithUID(uid: String, values:[String: AnyObject]) {
let ref = FIRDatabase.database().reference(fromURL:"")
let usersReference = ref.child("Users").child(uid)
usersReference.updateChildValues(values, withCompletionBlock: { (err,ref) in
if err != nil {
print(err as Any)
return
}
})
}
func handleSelectProfileImageView() {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
present(picker, animated: true, completion:nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker:UIImage?
if let editedImage = info["UIImagePickerControllerEditedImage"] as? UIImage{
selectedImageFromPicker = editedImage
}else if let originalImage = info["UIIMagePickerControllerOriginalImage"] as? UIImage{
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker{
profilePicture.image = selectedImage
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
print("canceled picker")
dismiss(animated: true, completion: nil)
}
}
答案 0 :(得分:2)
您正在viewDidLoad中的imageView中上传图片。因此,它总是会将您放在imageView中的图像作为占位符上传。
见这个例子:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
var selectedImageFromPicker: UIImage?
if let editedImage =
info["UIImagePickerControllerEditedImage"] as? UIImage {
selectedImageFromPicker = editedImage
} else if let originalImage =
info["UIImagePickerControllerOriginalImage"] as? UIImage{
selectedImageFromPicker = originalImage
}
if let selectedImage = selectedImageFromPicker {
updateAvatarImageInFirebase(image: selectedImage)
}
dismiss(animated: true, completion: nil)
}
func updateAvatarImageInFirebase(image: UIImage){
let user = FIRAuth.auth()?.currentUser
guard let uid = user?.uid else {
return
}
if user != nil {
let storageRef = FIRStorage.storage().reference().child("users").child(uid).child("profileImage.jpg")
if let uploadData = UIImageJPEGRepresentation(image, 0.2) {
storageRef.put(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil {
print(error!)
return
}
if let profileImageUrl = metadata?.downloadURL()?.absoluteString {
FIRDatabase.database().reference().child("users").child(uid).updateChildValues(["profileImageUrl": profileImageUrl])
}
})
}
}
}