Agn*_*Dev 23 objective-c uiimage sdwebimage
您好我在项目中使用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];
}
}];
Run Code Online (Sandbox Code Playgroud)
有什么我错过了吗?看起来在我的分配工具中这样做会增加大量内存.
Sta*_*org 27
我很惊讶没有人回答这个问题,但是我遇到了类似的问题并且遇到了这个问题,所以我会回答这个问题,因为有人在看这个问题(假设你现在已经把它排除了).
要直接回答您的问题,是的,您要将图像缓存两次.
下载对SDWebImageManager的调用会根据图像url的absoluteString自动使用键缓存图像.如果你想要自己的密钥,可以在SDWebImageDownloader上使用下载调用,据我所知,默认情况下不会缓存.从那里你可以像你正在做的那样调用sharedImageCache并使用你想要的任何键进行缓存.
除此之外,在任何情况下,由于SDWebImage喜欢缓存到磁盘而不是内存,因此看到分配堆积很奇怪.也许还有其他事情在同一时间发生?
希望这可以帮助,
布兰登
小智 19
在Swift中使用下面的代码下载图像并将其存储在缓存中:
//SDWebImageManager download image with High Priority
SDWebImageManager.sharedManager().downloadImageWithURL(NSURL(string: imageUrl), options: SDWebImageOptions.HighPriority, progress: { (receivedSize :Int, ExpectedSize :Int) in
SVProgressHUD.show()
}, completed: { (image :UIImage!, error:NSError!, cacheType :SDImageCacheType, finished :Bool,imageUrl: NSURL!) in
if(finished) {
SVProgressHUD.dismiss()
if((image) != nil) {
//image downloaded do your stuff
}
}
})
Run Code Online (Sandbox Code Playgroud)
Swift 3版本:
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
})
Run Code Online (Sandbox Code Playgroud)
Objective-C版本:
[[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];
}
}];
Run Code Online (Sandbox Code Playgroud)
Doe*_*ata 10
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
}
)
Run Code Online (Sandbox Code Playgroud)
试试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 {.
}
})
Run Code Online (Sandbox Code Playgroud)
斯威夫特4:
SDWebImageManager.shared().loadImage(
with: URL(string: imageUrl),
options: .highPriority,
progress: nil) { (image, data, error, cacheType, isFinished, imageUrl) in
print(isFinished)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
40783 次 |
最近记录: |