我正在尝试下载多个图像以显示在 Collection View 单元格中。但是只下载一个就超过了下载大小。如果我将下载大小升级到更高的值,则应用程序会在下载 3 个或更多图像后崩溃。如何有效地下载图像并将它们显示在我的收藏 View 中?
这是我要上传的代码:
func uploadImage(_ image: UIImage, uid: String,categoryIndex:Int, spotIndex:Int,completion: @escaping ((_ url: URL?) ->())) {
let storageReference = Storage.storage().reference().child("user/\(uid)/\(categoryIndex)/\(spotIndex).jpg")
guard let imageData = UIImage(data: image.jpegData(compressionQuality: 0.8)!) else { return }
let metaData = StorageMetadata()
metaData.contentType = "img/jpg"
storageReference.putData(imageData.jpegData(compressionQuality: 0.8)!, metadata: metaData, completion: { metaData, error in
if error == nil, metaData != nil {
// success
storageReference.downloadURL(completion: { (url, error) in
guard let downloadURL = url else {
print("ERROR in image link")
return
}
completion(downloadURL)
})
} else {
// Fail
completion(nil)
}
})
}
这是我要下载的代码:
// Download image using the category index and spot index to get the correct image
func downloadImages(folderPath: String, categoryIndex: Int, spotIndex: Int,success: @escaping (_ image: UIImage)->(), failure:@escaping (_ error:Error)->()) {
let reference = Storage.storage().reference(withPath: "\(folderPath)/\(categoryIndex)/\(spotIndex).jpg")
reference.getData(maxSize: (1 * 1024 * 1024)) { (data, error) in
if let error = error {
print(error.localizedDescription)
failure(error)
} else {
if let data = data {
let myImage:UIImage! = UIImage(data: data)
success(myImage)
}
}
}
}
最佳答案
我找到了解决方案。压缩质量应低于 0.8 我使用了 0.25 并且似乎工作得很好。
关于swift - 如何从 firebase 下载多个图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51937167/