在Firebase中使用分配给用户配置文件的图像并将其附加到配置文件页面 [英] using the image assigned to userprofile in firebase and attachine it to profile page

查看:66
本文介绍了在Firebase中使用分配给用户配置文件的图像并将其附加到配置文件页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上寻找帮助,但我无法解决.下面的代码说,当用户注册时,该图像允许将图像存储在Firebase数据库中:

I have been looking online everywhere to help me out here but I cannot figure this out. The code below says that allows for the image to get stored in the firebase database when the user registers:

func uploadProfileImage(_ image: UIImage, completion: @escaping ((_ url: URL?)->())){
    guard let uid = Auth.auth().currentUser?.uid else {return}
    let storageRef = Storage.storage().reference().child("user/\(uid)")

    guard let imageData = image.jpegData(compressionQuality: 0.75 ) else {return}
    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg."


    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        if error == nil, metaData != nil {
            storageRef.downloadURL { url, error in
                completion(url)
                // success!
            }
        } else {
            // failed
            completion(nil)
        }
    }
}

func saveProfile(name: String, email: String, profileImageURL: URL, completion: @escaping ((_ success:Bool)->())){
    guard let uid = Auth.auth().currentUser?.uid else {return}

    let databaseRef = Database.database().reference().child("users/profile/\(uid)")


    let userObject = ["name":name, "email":email,
                      "photoURL":profileImageURL.absoluteString] as [String:Any]

    databaseRef.setValue(userObject) {error, ref in completion(error == nil)}
    }
}

此代码在另一个.swift文件中,可以让我获取照片:

This code in a different .swift file allows me to get the photo:

static let cache = NSCache<NSString, UIImage>()

static func downloadImage(withURL url:URL, completion: @escaping (_ image:UIImage?, _ url:URL)->()) {
    let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in
        var downloadedImage:UIImage?

        if let data = data {
            downloadedImage = UIImage(data: data)
        }

        if downloadedImage != nil {
            cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString)
        }

        DispatchQueue.main.async {
            completion(downloadedImage, url)
        }

    }

    dataTask.resume()
}

static func getImage(withURL url:URL, completion: @escaping (_ image:UIImage?, _ url:URL)->()) {
    if let image = cache.object(forKey: url.absoluteString as NSString) {
        completion(image, url)
    } else {
        downloadImage(withURL: url, completion: completion)
    }
}

用户个人资料代码:

class UserProfile {

var uid:String
var name:String
var photoURL:URL

init(uid:String, name:String, photoURL:URL){

    self.uid = uid
    self.name = name
    self.photoURL = photoURL



}

}

我添加了一个文件,该文件可让我从firebase中获取数据:

I have added a file that will allow me to get the data from firebase:

class ImageServices{

static let cache = NSCache<NSString, UIImage>()

static func downloadImage(withURL url:URL, completion: @escaping (_ image:UIImage?, _ url:URL)->()) {
    let dataTask = URLSession.shared.dataTask(with: url) { data, responseURL, error in
        var downloadedImage:UIImage?

        if let data = data {
            downloadedImage = UIImage(data: data)
        }

        if downloadedImage != nil {
            cache.setObject(downloadedImage!, forKey: url.absoluteString as NSString)
        }

        DispatchQueue.main.async {
            completion(downloadedImage, url)
        }

    }

    dataTask.resume()
}

static func getImage(withURL url:URL, completion: @escaping (_ image:UIImage?, _ url:URL)->()) {
    if let image = cache.object(forKey: url.absoluteString as NSString) {
        completion(image, url)
    } else {
        downloadImage(withURL: url, completion: completion)
    }
}

}

现在如何在我的viewController中使用它在imageView上实际显示图像?

Now how do I use this in my viewController to actually show the image on the imageView???

推荐答案

问题的最后编辑要求

现在如何在我的viewController中使用它来实际显示图像 在imageView上??

Now how do I use this in my viewController to actually show the image on the imageView???

这表示您在询问如何将下载的图像分配给该imageView时,其中的viewController包含一个imageView.

This indicates you have a viewController that contains an imageView as you're asking how to assign the downloaded image to that imageView.

图片已通过您的代码下载到此处

The image is downloaded here in your code

static func downloadImage(withURL...
        var downloadedImage:UIImage
        if let data = data {
            downloadedImage = UIImage(data: data)

因此,下面的一行代码将获取downloadedImage并将其显示在imageView中

so a single line of code following that will take the downloadedImage and display it in the imageView

self.myImageView.image = downloadedImage

这篇关于在Firebase中使用分配给用户配置文件的图像并将其附加到配置文件页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆