ios - 将 PNG 图像从 Firestore 设置为 UIImage.image --- Swift
问题描述
我正在尝试将此 UIImageView 图像(profileImage)设置为我从 Firestore 下载的。我没有收到任何错误,它运行良好,但 profileImage 包含视图背景颜色,而不是从 Firestore 下载的图像。
storage = Storage.storage().reference(forURL: "gs://senlink-6d966.appspot.com")
profilePath = storage.child("users_profile/\(Auth.auth().currentUser!.uid).png")
profilePath?.downloadURL(completion: { (url, error) in
if error != nil {
print("\(error!)")
} else {
if let url = url {
DispatchQueue.main.async {
let data = UIImage(data: url.absoluteURL.dataRepresentation)
self.profileImg.image = data
}
}
}
})
解决方案
该downloadURL
方法返回图像数据的 url,而不是实际的图像数据。要直接下载图像数据,您可以使用getData
. 这是文档中的示例:
// Create a reference to the file you want to download
let islandRef = storageRef.child("images/island.jpg")
// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
islandRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Data for "images/island.jpg" is returned
let image = UIImage(data: data!)
}
}
这应该做你想要的:
storage = Storage.storage().reference(forURL: "gs://senlink-6d966.appspot.com")
profilePath = storage.child("users_profile/\(Auth.auth().currentUser!.uid).png")
profilePath?.getData(maxSize: 1 * 1024 * 1024) { data, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
// Data for your image
let image = UIImage(data: data!)
DispatchQueue.main.async {
self.profileImg.image = image
}
}
}
推荐阅读
- javascript - Firefox 78.0.1 响应式设计模式在屏幕旋转后没有为 window.innerWidth 返回正确的值
- google-kubernetes-engine - Anthos 服务网格指标
- c - 内核模块拦截 nfs 方法并返回自定义错误代码
- file - 气流、文件夹传感器和任务触发器
- javascript - 如何在同一个发布请求中多次使用地理编码功能(npm:node-geocoder)?
- javascript - 需要创建一个函数,该函数从数组中获取一个值,将其存储,然后仅使用 .pop 和 .push 将其放置在第二个数组中
- javascript - 无法将组件连接到 Redux 存储
- python - 如何在 mnist tensorflow python 中获得预测的类标签?
- python - 如何在opencv python中为圆设置动画以在矩形路径中移动?
- powershell - 比较 CSV 中的 2 列,如果匹配则创建一个新列