Firebaseから写真のユーザアカウントをダウンロードしようとしています。 私の問題について説明します: ユーザーのプロフィールページには、自分のプロフィールアバターの現在の画像を表示するボタンがあります。ボタンの背景には画像がありません。最初にクリックすると写真の画像を選択できますライブラリーを作成したり、デバイスカメラ(アラートコントローラー)で写真を撮ると、この画像または写真がFirebase Storageにアップロードされ、新しい写真がボタンの背景に表示されます。Firebase Storageから写真をダウンロードし、現在のユーザの最後のものを交換する方法
しかし、ユーザーが現在の写真を変更したいときは、もう一度ボタンをタップして写真を撮るか、写真を撮っても、ボタンの背景は変わらない。現在のページの写真では変更は更新されませんが、別のビューに戻って写真の変更ビューに戻ると、ボタンの背景に選択したユーザーの正しい写真が表示されます。したがって、ダウンロードはうまくいくが、ユーザーが別の見方をしなかった場合はすぐにはうまくいかない。説明するのは少し難しい。ここで
は私のアップロードの方法のための私のコードです:ダウンロードした後
// Variables I need
@IBOutlet weak var photoProfil: UIButton!
var name: String!
var userUid: String!
@IBOutlet var userImagePicker: UIImageView!
var imagePicker: UIImagePickerController!
var imageSelected = false
var isUploaded: Bool = false
var refDatabase: DatabaseReference!
var photovide: UIImage = UIImage(named:"profilvide")!
// Take a photo with the camera (first choice)
func takePhoto(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.camera // Source Camera
self.present(imagePicker, animated: false, completion: nil)
} else { // If camera doesn't work
imagePicker.delegate = self
imagePicker.allowsEditing = false
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary // Source Library
self.present(imagePicker, animated: false, completion: nil)
}
}
// Choose an image from the library
func libraryPhoto(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.photoLibrary
image.allowsEditing = false
self.present(image, animated: true)
}
// Take this image and shows it on the View Controller
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
// I want to upload the photo to storage and save it in the database
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
photoProfil.setImage(image, for: .normal) // Button showing the user photo
userImagePicker.image = image
imageSelected = true
uploadImg()
} else {
print("Error...")
}
imagePicker.dismiss(animated: true, completion: nil)
self.dismiss(animated: true, completion: nil)
}
// The alert Controller
@IBAction func actionButton(_ sender: Any) {
let attributedString = NSAttributedString(string: "User photo", attributes: [
NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15),
NSForegroundColorAttributeName : UIColor.black
])
let alertController = UIAlertController(title: "", message: "", preferredStyle: .actionSheet)
alertController.message = nil
alertController.setValue(attributedString, forKey: "attributedTitle")
alertController.addAction(UIAlertAction(title: "Take photo", style: .default, handler: self.takePhoto))
alertController.addAction(UIAlertAction(title: "Choose in Library", style: .default, handler: self.libraryPhoto))
alertController.addAction(UIAlertAction(title: "Show current photo", style: .default, handler: self.showPhoto))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
// Create a path in order to save the photo in Firebase
func setUser(img: String) {
var userUid = Auth.auth().currentUser?.uid
let userData = [
"nickname": Auth.auth().currentUser?.displayName,
"userImg": img
]
KeychainWrapper.standard.set(userUid!, forKey: "uid")
let location = Database.database().reference().child("users").child(userUid!).child("pseudo")
location.setValue(userData)
dismiss(animated: true, completion: nil)
}
// Upload and put image in the Firebase Storage
func uploadImg() {
guard let img = userImagePicker.image, imageSelected == true else {
print("Image needs to be selected")
return
}
if let imgData = UIImageJPEGRepresentation(img, 0.2) {
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
Storage.storage().reference().child(imgUid).putData(imgData, metadata: metadata) { (metadata, error) in
if error != nil {
print("Did not upload img")
self.isUploaded = false
} else {
print("uploaded")
self.isUploaded = true
let downloadURL = metadata?.downloadURL()?.absoluteString
if let url = downloadURL {
self.setUser(img: url)
}
}
}
}
}
はここでダウンロード方法およびshow写真のためのコードです:
override func viewDidLoad() {
super.viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
// For download photo from Firebase
name = Auth.auth().currentUser?.displayName
userUid = Auth.auth().currentUser?.uid
refDatabase = Database.database().reference().child("users").child(userUid!)
refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("userImg") {
self.isUploaded = true
print("True userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("isUploaded2 = false")
print("false userImg doesn't exist")
print("Aucune photo n'a été uploadée dans la base de données")
}
})
}
override func viewWillAppear(_ animated: Bool) {
self.userImagePicker.image = nil
guard let username = Auth.auth().currentUser?.displayName else { return }
self.navigationItem.title = username
refDatabase = Database.database().reference().child("users").child(userUid!)
refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("userImg") {
self.isUploaded = true
print("isUploaded2 = true")
print("true userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("False userImg doesn't exist")
}
})
}
func downloadPhoto(user: String) {
self.name = user
let recipientData = Database.database().reference().child("users").child(userUid!).child("pseudo")
recipientData.observeSingleEvent(of: .value, with: { (snapshot) in
let data = snapshot.value as! Dictionary<String, AnyObject>
let nickname = data["nickname"]
let userImg = data["userImg"]
let ref = Storage.storage().reference(forURL: userImg! as! String)
ref.getData(maxSize: 1000000, completion: { (data, error) in
if error != nil {
print("Could not load image")
} else {
if let imgData = data {
if let img = UIImage(data: imgData) {
self.userImagePicker.image = img
self.photoProfil.setImage(img, for: .normal)
}
}
}
})
})
userImagePicker.reloadInputViews()
}
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}
// Try to do the same with the viewDidLoad() method for download but can't work
override func viewDidAppear(_ animated: Bool) {
refDatabase = Database.database().reference().child("users").child(userUid!)
refDatabase.child("pseudo").observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.hasChild("userImg") {
self.isUploaded = true
print("True userImg exists")
self.downloadPhoto(user: self.name)
} else {
self.isUploaded = false
self.userImagePicker = UIImageView(image: self.photovide)
self.photoProfil.setImage(self.photovide, for: .normal)
print("False userImg doesn't exist")
}
})
}
'didFinishPickingMediaWithInfo'メソッドの中に' photoProfil.setImage(image、for:.normal) 'の前に' print(image) 'を追加すると、何が印刷されますか?このメソッドが呼び出されていることを確認できるようにしました。 – 3stud1ant3
私はそれを行いました。印刷(イメージ)を追加しましたが、何も表示されず、print( "image")で置き換えて出力します。 –
' if let文? – 3stud1ant3