소셜 미디어 앱을 제작 중입니다. 프로필 페이지를 만들었습니다. Firebase 데이터베이스에 연결했습니다. 그러나 프로필 그림을 데이터베이스에서로드해야합니다.Firebase가 Swift로 URL을 반환하지 않습니다.
내 데이터베이스에는 프로필 pic url (firebase storage url)이 있으며 데이터베이스에서 가져 와서 프로필 사진에 이미지를 나타내려고합니다.
또한 3 가지 작업 요청을 만들었습니다. 사용자가 사용할 수있는 사진 3 옵션을 클릭하면
이때문에 나는 또한 내 이미지가 다른보기 여기
에 전달하고자하는 그 쇼의 사진
일부 코드
카메라를 사용하여 새 사진을 촬영 갤러리
에서 새로운 사진을 선택 :override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
profilePic.layer.cornerRadius = profilePic.frame.size.width/2
profilePic.clipsToBounds = true
getImages()
}
func getImages(){
if FIRAuth.auth()?.currentUser?.uid==nil{
self.performSegue(withIdentifier: "logOutSegue", sender: self)
} else {
let uid = FIRAuth.auth()?.currentUser?.uid
databaseRef.child("Users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
if let dict = snapshot.value as? NSDictionary{
let ImageUrl = dict["Profpic"] as? String
print(ImageUrl)
let url = URL(string: ImageUrl!)
print(url)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
if error != nil{
print(error?.localizedDescription)
return
}
DispatchQueue.main.async {
self.profilePic?.image = UIImage(data: data!)
}
}).resume()
}
})
}
}
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
self.profilePic.image = image
picker.dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}
@IBAction func changePic(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
let actionSheet = UIAlertController(title: "Foto Kaynağı", message: "Bir kaynak seçin", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Kameranla yeni bir fotoğraf çek", style: .default, handler: { (action:UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera){
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
}
else {
print("Camera not available")
}
}))
actionSheet.addAction(UIAlertAction(title: "Galeriden yeni bir fotoğraf seç", style: .default, handler: { (action:UIAlertAction) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}))
actionSheet.addAction(UIAlertAction(title: "Fotoğrafı Göster", style: .default, handler: { (action:UIAlertAction) in
self.performSegue(withIdentifier: "showProfilePic", sender: self)
}))
actionSheet.addAction(UIAlertAction(title: "Geri Dön", style: .cancel, handler: nil))
self.present(actionSheet, animated: true,completion: nil)
}
@IBAction func saveClicked(_ sender: Any) {
let imageName = NSUUID().uuidString
let storedImage = storageRef.child("Profile Pictures").child(imageName)
if let uploadData = UIImagePNGRepresentation(self.profilePic.image!){
storedImage.put(uploadData, metadata: nil, completion: { (metadata, error) in
if error != nil {
print(error?.localizedDescription)
return
}
storedImage.downloadURL(completion: { (url, error) in
if error != nil {
print(error?.localizedDescription)
return
}
if let urlText = url?.absoluteString{
self.databaseRef.child("Users").child((FIRAuth.auth()?.currentUser?.uid)!).updateChildValues(["ProfPic" : urlText], withCompletionBlock: { (error, ref) in
if error != nil{
print(error?.localizedDescription)
return
}
})
}
})
})
}
}
db 구조를 표시 할 수 있습니까? observeSingleEvent 트리거도 수행합니까? – Christian
인쇄 명세서가 예상되는 값을 출력합니까? 여기에 어떤 문제가 있습니까? 당신은 당신의 문제와 질문을보다 명확하게해야합니다. –
내 print 문은 nil을 반환합니다. 나는 또한 그것을 언급했다 –