gpt4 book ai didi

ios - 下载 Firebase 存储的 URL 不起作用

转载 作者:行者123 更新时间:2023-12-04 07:14:17 25 4
gpt4 key购买 nike

我正在将 firebase 用于聊天应用程序,并且我已经将图像存储在 firebase 存储中,现在我想获取 url 以获取用户个人资料,但即使我尝试过它也无法正常工作,但没有任何效果
这是第 3 行的函数,它生成错误(“DatabaseReference”类型的值没有成员“downloadURL”)

 func downloadURL(for path: String, completion: @escaping (Result<URL, Error>) -> Void) {
let refrence = database.child(path)


refrence.downloadURL(completion: { url, error in //here downnloadUrl not working perfectly
guard let url = url, error == nil else {
completion(.failure(StorageErrors.failedToGetDownloadUrl))
return
}
completion(.success(url))
})
}
网络服务类
struct NetworkingService{
static let shared = NetworkingService()
private init(){

}
private let database = Database.database().reference() //creating refrence

public func test(){
database.child("name").setValue(["faheem":true])
}


static func emailForImage(emailAddress: String) -> String{

var safeEmail = emailAddress.replacingOccurrences(of: ".", with: "-")
safeEmail = safeEmail.replacingOccurrences(of: "@", with: "-")
return safeEmail
}


func downloadURL(for path: String, completion: @escaping (Result<URL, Error>) -> Void) {
let refrence = database.child(path)
refrence.downloadURL(completion: { url, error in
guard let url = url, error == nil else {
completion(.failure(StorageErrors.failedToGetDownloadUrl))
return
}
completion(.success(url))
})
}


func userExists(email: String,completion: @escaping((Bool) -> Void)){ //return boolen

var safeEmail = email.replacingOccurrences(of: ".", with: "-")
safeEmail = safeEmail.replacingOccurrences(of: "@", with: "-")

database.child(safeEmail).observeSingleEvent(of: .value) { (snapshot) in


guard let foundEmail = snapshot.value as? String else {
completion(false)
return
}
}
completion(true)
}


func insertUser(user: CUser, completion: @escaping(Result<CUser,Error>) -> Void){
database.child(user.identifier).setValue(user.getDict) { (err, dbRef) in

if err != nil{
completion(.failure(err!))
}else{
completion(.success(user))
}
}
}



}
StoreManager 类
struct StoreManager{

static let shared = StoreManager()
private let storageRefrence = Storage.storage().reference()

func uploadProfilePic(data: Data, fileName: String, completion: @escaping(Result<String,Error>) -> Void){
storageRefrence.child("images/\(fileName)").putData(data,metadata: nil) { (metaData, error) in

guard error == nil else {
completion(.failure(AppError.failedToUpload))
print("faheem ye he error \(error?.localizedDescription)")
return
}

self.storageRefrence.child("images/\(fileName)").downloadURL { (url, error) in
guard let url = url else {
completion(.failure(AppError.failedtoDownloadUrl))
return
}
let urlString = url.absoluteString
print("download url is\(urlString)")
completion(.success(urlString))
}
}

}
}
注册用户类
class SignUpUserViewController: UIViewController {


@IBOutlet weak var userProfile: UIImageView!
@IBOutlet weak var lname: UITextField!
@IBOutlet weak var fname: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var password: UITextField!
var imagePicker = UIImagePickerController() // for imagepicker
private let spinner = JGProgressHUD(style: .dark)
var user:CUser!
override func viewDidLoad() {
super.viewDidLoad()

password.isSecureTextEntry = true
userProfile.layer.masksToBounds = true
userProfile.layer.borderWidth = 2
userProfile.layer.borderColor = UIColor.lightGray.cgColor
userProfile.layer.cornerRadius = userProfile.frame.size.height / 2
// for rounder image

userProfile.isUserInteractionEnabled = true /
let gesture = UITapGestureRecognizer(target: self, action: #selector(userProfileChange)) //photopcker
userProfile.addGestureRecognizer(gesture)

}

@objc private func userProfileChange(){
print("profile pic changed")
getPhoto()

}
private func insertUser(_ user:CUser){
NetworkingService.shared.insertUser(user: user) { (result) in
switch result{
case .success(let user):
print("User inserted: ", user.getDict)
CUser.shared = user
print("sahed Data is fahem\(CUser.shared)")
case .failure(let error):
print("Error: ",error.localizedDescription)
self.showAlert(message: error.localizedDescription)
}
}
}
private func createUser(_ user:CUser){
FirebaseAuth.Auth.auth().createUser(withEmail: user.email, password: user.password) { [weak self] (respomse, error) in
guard let data = respomse else {
print("Error: ",error?.localizedDescription ?? "")
self?.showAlert(message: error?.localizedDescription ?? "")
return
}
self?.insertUser(user)
}
}

private func uploadImage(_ imageData:Data) {

StoreManager.shared.uploadProfilePic(data: imageData, fileName: user.imageName) { (result) in
switch result{
case .success(let url):
self.user.imageURL = url
self.createUser(self.user)
case .failure(let error):
print("Error: ",error.localizedDescription)
self.showAlert(message: error.localizedDescription)
}
}
}

// register User
@IBAction func register(_ sender: UIButton) {
if checkTextFields([email,password,fname,lname]) {
showAlert(message: "Some fields are missing")
}else{
spinner.show(in: self.view)
NetworkingService.shared.userExists(email: email.text!) { (response) in//checking user through email
self.spinner.dismiss()
guard response != nil else { //measn user exists
self.showAlert(message: "user account already exits we varify through email")
return
}
self.user = CUser(firstName: self.fname.text!, lastName: self.lname.text!, email: self.email.text!, password: self.password.text!, imageURL: "")
/*image url is nil becuase we not upload the image here wehn calling the upload image method
then after setting create user method here we only setting the data in user model and url nil*/
if let userImage = self.userProfile.image!.jpegData(compressionQuality: 0.2) {
self.uploadImage(userImage)
}else{
print("Error: Image cannot be compressed")
}
}

}
}


func checkTextFields(_ textfields:[UITextField]) -> Bool{
for textfield in textfields {
return textfield.text?.isEmpty == true ? true: false
}
return true
}
func gotOLogin(){
guard let loginController = self.storyboard?.instantiateViewController(withIdentifier: String(describing: LoginUserViewController.self)) as? LoginUserViewController else {return}
self.navigationController?.pushViewController(loginController, animated: true)
}
func showAlert(title:String = "Error", message:String){
let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
let ok = UIAlertAction(title: "ok", style:.default, handler: nil)
alertController.addAction(ok)
self.present(alertController, animated: true, completion: nil)
}

}


extension SignUpUserViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate{

func getPhoto(){
let actionSheet = UIAlertController(title: "Choose Image", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
actionSheet.addAction(UIAlertAction(title: "Take Photo", style: .default, handler: {
[weak self] _ in
self?.presentCamera()
}))
actionSheet.addAction(UIAlertAction(title: "Choose Photo", style: .default, handler: { [weak self] _ in

self?.profilePhotoPicker()
}))
present(actionSheet, animated: true, completion: nil)
}

func presentCamera(){ //not allowed in simualtor to capture photo
imagePicker.sourceType = .camera
imagePicker.delegate = self
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
}

func profilePhotoPicker(){
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)

}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { //called when select photo
print(info)
userProfile.image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
imagePicker.dismiss(animated: true, completion: nil)
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)

}

}
ProfileViewcontroler 这里是 getProfilePic 乐趣我已经得到了所有必要的东西,但问题是在网络类下载 url 不起作用
class ProfileViewController: UIViewController {

var logoutUser = ["lgout"]
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var currentUserPic: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: String(describing: ProfileCell.self), bundle: .main), forCellReuseIdentifier: String(describing: ProfileCell.self))
}

func getProfilePic(){
guard let userEmail = UserDefaults.standard.value(forKey: "useremail") as? String else {
return}

let userEmailForPic = NetworkingService.emailForImage(emailAddress: userEmail)
let fileName = userEmailForPic + "_profile_image_jpg"
let patheStorage = "images/"+fileName//here i want to fetch the pic but in netwroking class downnload url not working


}


}

最佳答案

Firebase 实时数据库和 Firebase 存储是独立的服务。您应该使用 downloadURL关于存储引用:

func downloadURL(for path: String, completion: @escaping (Result<URL, Error>) -> Void) {
let reference = Storage.storage().reference().child(path)

reference.downloadURL(completion: { url, error in
guard let url = url, error == nil else {

completion(.failure(StorageErrors.failedToGetDownloadUrl))
return
}
completion(.success(url))
})
}

关于ios - 下载 Firebase 存储的 URL 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68881184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com