using the image assigned to userprofile in firebase and attachine it to profile page

自作多情 提交于 2020-05-24 05:48:11

问题


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)}
    }
}

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)
    }
}

user profile code:

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



}

}

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)
    }
}

}

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


回答1:


The last edit to the question asks this

Now how do I use this in my viewController to actually show the image on the 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)

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

self.myImageView.image = downloadedImage


来源:https://stackoverflow.com/questions/61679954/using-the-image-assigned-to-userprofile-in-firebase-and-attachine-it-to-profile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!