From c3815069327ef45782a366b4669398ef7575b1bc Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 3 Jan 2019 14:15:27 +0800 Subject: [PATCH 1/2] Fix the current image cost (pixels) for FLAnimatedImage, with a more correct value for memory cost function --- .../FLAnimatedImageView+WebCache.m | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m index fc39bafb..3de37d3a 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -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,16 @@ 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 + + return animatedImageCost + imageCost; +} + @implementation UIImage (FLAnimatedImage) - (FLAnimatedImage *)sd_FLAnimatedImage { @@ -152,9 +163,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 +174,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; From 3b19e76c6ff323ae07775196946b1b6524b69fa3 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 3 Jan 2019 14:21:18 +0800 Subject: [PATCH 2/2] Update the code for UIAnimatedImage associated with FLAnimatedImage case...This can happen when enable `GIF coder` as well as using FLAnimatedImageView --- SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m | 1 + 1 file changed, 1 insertion(+) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m index 3de37d3a..7a00000d 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -37,6 +37,7 @@ static inline NSUInteger SDWebImageMemoryCostFLAnimatedImage(FLAnimatedImage *an 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; }