我正在从Firebase存储中以数据形式获取图像,我想使用SDWebImage在我的应用程序中缓存图像。请指导我如何实现它?
for x in images_list{
let storageRef = storage.child("images/\(x).jpg")
storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) in
if let error = error{
print(error.localizedDescription)
return
}
else{
imagesarray.append(UIImage(data: data!)!)
}
if let x = images_list.last{
cell.imageDemo.animationImages = imagesarray
cell.imageDemo.sd_setImage(with: storageRef) // Here I Want to cache the images of **imagesarray** that are being animated
cell.imageDemo.animationDuration = 2
cell.imageDemo.startAnimating()
}
}
}
答案 0 :(得分:1)
您正在直接从问题中的Firebase下载imageData。除了使用getData
之外,您还需要使用downloadURL
方法。像这样:
for x in image_list {
let storageRef = storage.child("images/\(x).jpg")
//then instead of downloading data download the corresponding URL
storageRef.downloadURL { url, error in
if let error = error {
} else {
//make your imageArray to hold URL rather then images directly
imagesArray.append(url)
}
}
}
//////This is were you would use those url////////
let url = imageArray.first!
imageView.sd_setImage(with: url, completed: nil)
这里最重要的部分是下载URL,而不是图像数据。并且每当您将图片网址设置为imageView时,SDWebImage库都会对其进行缓存并显示(如果已缓存)
答案 1 :(得分:0)
我使用getURL方法而不是getData方法并缓存了图像数组。
var imagesarray = [URL]()
for x in images_list{
let storageRef = storage.child("images/\(x).jpg")
storageRef.downloadURL { (url, error) in
if let error = error{
print(error.localizedDescription)
}
else{
imagesarray.append(url!)
}
if let x = images_list.last{
cell.imageDemo.sd_setAnimationImages(with: imagesarray)
cell.imageDemo.animationDuration = 2
cell.imageDemo.startAnimating()
}
}
}