我一直在网上寻找帮助我的地方,但是我无法弄清楚。下面的代码说,允许用户注册时将图像存储在Firebase数据库中:
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文件中,可以让我获取照片:
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中获取数据:
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上实际显示图像?
对问题的最后编辑要求此
现在如何在我的viewController中使用它来实际显示图像在imageView上???
这表示您在询问如何将下载的图像分配给该imageView时,其中的viewController包含imageView。
图像已在您的代码中下载到此处
static func downloadImage(withURL... var downloadedImage:UIImage if let data = data { downloadedImage = UIImage(data: data)
因此,下面的一行代码将采用downloadedImage并将其显示在imageView中
self.myImageView.image = downloadImage