Add update image memory cost method

This commit is contained in:
zhongwuzw 2018-12-09 11:56:51 +08:00
parent 45427b1eed
commit cff0f33430
3 changed files with 24 additions and 7 deletions

View File

@ -175,13 +175,7 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV
image.sd_FLAnimatedImage = animatedImage;
image.sd_memoryCost = SDWebImageCalculateFLAnimatedImageMemoryCost(animatedImage);
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:key];
// If image can be find in memory cache, we update the cost value of cost.
if (cachedImage) {
[[SDImageCache sharedImageCache] removeImageForKey:key fromDisk:NO withCompletion:^{
[[SDImageCache sharedImageCache] storeImage:image forKey:key toDisk:NO completion:nil];
}];
}
[[SDImageCache sharedImageCache] updateImageMemoryCostWithKey:key];
}
sstrongSelf.image = animatedImage.posterImage;
sstrongSelf.animatedImage = animatedImage;

View File

@ -160,6 +160,17 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot
*/
- (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key;
#pragma mark - Update Ops
/**
* Update image memory cost if image already be stored in memory.
*
*
* @param key The unique image cache key, usually it's image absolute URL
* @note After image be stored in memory cache, if image's memory size changes, such as set a large memory associated object to it, or subclass `UIImage` which has its own large memory properties, you can use this to update memory cost which can make memory cache handle `maxMemoryCost` correctly
*/
- (void)updateImageMemoryCostWithKey:(nullable NSString *)key;
#pragma mark - Query and Retrieve Ops
/**

View File

@ -357,6 +357,18 @@
}
}
#pragma mark - Update Ops
- (void)updateImageMemoryCostWithKey:(NSString *)key {
UIImage *cachedImage = [self imageFromMemoryCacheForKey:key];
// If image can be find in memory cache, we update the cost value of cost.
if (cachedImage) {
[self removeImageForKey:key fromDisk:NO withCompletion:^{
[self storeImage:cachedImage forKey:key toDisk:NO completion:nil];
}];
}
}
#pragma mark - Query and Retrieve Ops
- (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock {