您好我在项目中使用SDWebImage框架,我想下载和缓存图像,但我想我的代码是将图像存储在缓存中两次?有没有办法只用一次键将图像存储在缓存中?这是我的代码。
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:[NSURL URLWithString:url] options:0 progress:^(NSUInteger receivedSize, long long expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
if(image){
NSString *localKey = [NSString stringWithFormat:@"Item-%d", i];
[[SDImageCache sharedImageCache] storeImage:image forKey:localKey];
}
}];
我错过了什么吗?看起来在我的分配工具中这样做会增加大量内存。
答案 0 :(得分:26)
我很惊讶没有人回答这个问题,但是我遇到了类似的问题并且遇到了这个问题,所以我会回答这个问题,因为我们现在已经将这个问题排除在外了。
要直接回答您的问题,是的,您要将图像缓存两次。
下载对SDWebImageManager的调用会根据图像的url的absoluteString自动使用键缓存图像。如果你想要自己的密钥,可以在SDWebImageDownloader上使用下载调用,据我所知,默认情况下不会缓存。从那里你可以像你现在一样调用sharedImageCache并使用你想要的任何键进行缓存。
除此之外,在任何情况下看到分配都堆积起来很奇怪,因为SDWebImage喜欢缓存到磁盘而不是内存。也许还有其他事情在同一时间发生?
希望这有帮助,
布兰登
答案 1 :(得分:17)
在 Swift 中使用以下代码下载图片并将其存储在缓存中:
SDWebImageManager.shared().downloadImage(with: NSURL(string: "...") as URL!, options: .continueInBackground, progress: {
(receivedSize :Int, ExpectedSize :Int) in
}, completed: {
(image : UIImage?, error : Error?, cacheType : SDImageCacheType, finished : Bool, url : URL?) in
})
Swift 3 版本:
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:imageUrl] options:SDWebImageDownloaderUseNSURLCache progress:nil completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
if (image && finished) {
// Cache image to disk or memory
[[SDImageCache sharedImageCache] storeImage:image forKey:CUSTOM_KEY toDisk:YES];
}
}];
Objective-C 版本:
class C : public A, public B
{
void f();
};
答案 2 :(得分:5)
尝试使用 Swift 4.0 。
let url = URL(string: imageUrl!)
SDWebImageManager.shared().imageDownloader?.downloadImage(with: url, options: .continueInBackground, progress: nil, completed: {(image:UIImage?, data:Data?, error:Error?, finished:Bool) in
if image != nil {.
}
})
答案 3 :(得分:4)
尝试APSmartStorage(https://github.com/Alterplay/APSmartStorage)而不是SDWebImage。
APSmartStorage从网络获取数据,并以智能可配置的方式自动将数据缓存在磁盘或内存中。应该足够你的任务。
答案 4 :(得分:4)
SDWebImage将映像缓存到磁盘和内存。让我们来看看:
因此,您不必担心缓存。 SDWebImage很好地处理它。
如果您希望根据HTTP缓存标头进行设置,也可以执行缓存的自定义实现以及缓存中的图像刷新。
您可以在他们的github页面here上找到完整的详细信息。
答案 5 :(得分:4)
快捷键4:
SDWebImageManager.shared().loadImage(
with: URL(string: imageUrl),
options: .highPriority,
progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
print(isFinished)
}
答案 6 :(得分:1)
SWIFT 5和最新的SDWebImage 5.2.3
SDWebImageManager.shared.loadImage(
with: album.artUrlFor(imageShape: .square),
options: .continueInBackground, // or .highPriority
progress: nil,
completed: { [weak self] (image, data, error, cacheType, finished, url) in
guard let sself = self else { return }
if let err = error {
// Do something with the error
return
}
guard let img = image else {
// No image handle this error
return
}
// Do something with image
}
)
答案 7 :(得分:0)
//创建一个CUSTOME IMAGEVIEW并通过ARRAY传递图像URL(INDEXPATH.ROW)
(void) loadImage:(NSString *)imageLink{
imageLink = [imageLink stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager loadImageWithURL:[NSURL URLWithString:imageLink] options:SDWebImageDelayPlaceholder progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {
imageFrame.image = image;
}];
}
答案 8 :(得分:0)
是的,可以使用SDWebImage
下载图像并手动存储到本地内存中。
使用SDWebImage
func saveImage(url: URL, toCache: UIImage?, complation: @escaping SDWebImageNoParamsBlock) {
guard let toCache = toCache else { return }
let manager = SDWebImageManager.shared()
if let key = manager.cacheKey(for: url) {
manager.imageCache?.store(toCache, forKey: key, completion: complation)
}
}
使用图像网址从内存中加载图像
static func imageFromMemory(for url: String) -> UIImage? {
if let encoded = url.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed),
let url = URL(string: encoded) {
let manager = SDWebImageManager.shared()
if let key: String = manager.cacheKey(for: url),
let image = manager.imageCache?.imageFromMemoryCache(forKey: key) {
return image
}
}
return nil
}
答案 9 :(得分:0)
Swift 5.1
import UIKit
import SDWebImage
extension UIImageView{
func downloadImage(url:String){
//remove space if a url contains.
let stringWithoutWhitespace = url.replacingOccurrences(of: " ", with: "%20", options: .regularExpression)
self.sd_imageIndicator = SDWebImageActivityIndicator.gray
self.sd_setImage(with: URL(string: stringWithoutWhitespace), placeholderImage: UIImage())
}
}
使用方法
let myUrl = "https://www.example.com"
myImageView.downloadImage(url: myUrl)
答案 10 :(得分:-1)
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:ImageUrl options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
if(image){
NSLog(@"image=====%@",image);
}
}];