我正在尝试下载多个图像以显示在集合视图单元格中。但是,仅下载一个就超过了下载大小。如果我将下载大小升级为更高的值,则在下载3张或更多图片后,应用程序将崩溃。如何下载图像并将其有效显示在我的收藏夹视图中?
这是我要上传的代码:
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 :(得分:1)
我找到了解决方案。压缩质量应低于0.8 我使用0.25,似乎工作正常。