Merge pull request #2558 from dreampiggy/bugfix_memory_cost_animating

Fix the current image cost (pixels) for FLAnimatedImage
This commit is contained in:
Kinarobin 2019-01-25 18:56:05 +08:00 committed by GitHub
commit 35763e3376
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 2 deletions

View File

@ -15,6 +15,7 @@
#import "NSData+ImageContentType.h"
#import "UIImageView+WebCache.h"
#import "UIImage+MultiFormat.h"
#import "UIImage+MemoryCacheCost.h"
static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageView *imageView, NSData *imageData) {
if ([NSData sd_imageFormatForImageData:imageData] != SDImageFormatGIF) {
@ -30,6 +31,17 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV
return animatedImage;
}
static inline NSUInteger SDWebImageMemoryCostFLAnimatedImage(FLAnimatedImage *animatedImage, UIImage *image) {
NSUInteger frameCacheSizeCurrent = animatedImage.frameCacheSizeCurrent; // [1...frame count], more suitable than raw frame count because FLAnimatedImage internal actually store a buffer size but not full frames (they called `window`)
NSUInteger pixelsPerFrame = animatedImage.size.width * animatedImage.size.height; // FLAnimatedImage does not support scale factor
NSUInteger animatedImageCost = frameCacheSizeCurrent * pixelsPerFrame;
NSUInteger imageCost = image.size.height * image.size.width * image.scale * image.scale; // Same as normal cost calculation
imageCost = image.images ? (imageCost * image.images.count) : imageCost;
return animatedImageCost + imageCost;
}
@implementation UIImage (FLAnimatedImage)
- (FLAnimatedImage *)sd_FLAnimatedImage {
@ -152,9 +164,9 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV
__strong typeof(wweakSelf) sstrongSelf = wweakSelf;
if (!sstrongSelf || ![url isEqual:sstrongSelf.sd_imageURL]) { return ; }
// Step 3. Check if data exist or query disk cache
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
__block NSData *gifData = imageData;
if (!gifData) {
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
gifData = [[SDImageCache sharedImageCache] diskImageDataForKey:key];
}
// Step 4. Create FLAnimatedImage
@ -163,8 +175,12 @@ static inline FLAnimatedImage * SDWebImageCreateFLAnimatedImage(FLAnimatedImageV
if (![url isEqual:sstrongSelf.sd_imageURL]) { return ; }
// Step 5. Set animatedImage or normal image
if (animatedImage) {
if (sstrongSelf.sd_cacheFLAnimatedImage) {
if (sstrongSelf.sd_cacheFLAnimatedImage && SDImageCache.sharedImageCache.config.shouldCacheImagesInMemory) {
image.sd_FLAnimatedImage = animatedImage;
image.sd_memoryCost = SDWebImageMemoryCostFLAnimatedImage(animatedImage, image);
// Update the memory cache
[SDImageCache.sharedImageCache removeImageForKey:key fromDisk:NO withCompletion:nil];
[SDImageCache.sharedImageCache storeImage:image forKey:key toDisk:NO completion:nil];
}
sstrongSelf.image = animatedImage.posterImage;
sstrongSelf.animatedImage = animatedImage;