diff --git a/CHANGELOG.md b/CHANGELOG.md index 523ec928..649eaad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,14 +36,12 @@ See [all tickets marked for the 5.0.0 release](https://github.com/rs/SDWebImage/ - Rename `sd_UTTypeFromSDImageFormat` to `sd_UTTypeFromImageFormat` #2395 - Change `SDImageFormat` to use `NS_TYPED_EXTENSIBLE_ENUM` instead of fixed enum, to allow custom coder plugins to extend it #2400 -## [4.4.3 - 4.4 patch, on Nov 12th, 2018](https://github.com/SDWebImage/SDWebImage/releases/tag/4.4.3) +## [4.4.3 - 4.4 patch, on Nov 25th, 2018](https://github.com/SDWebImage/SDWebImage/releases/tag/4.4.3) See [all tickets marked for the 4.4.3 release](https://github.com/SDWebImage/SDWebImage/milestone/28) #### Fixes - Revert the hack code for `FLAnimatedImage`, because of the `FLAnimatedImage` initializer method blocks the main queue #2441 -- Optimize when scale = 1 #2520 - Fix extention long length of file name #2516 6c6d848 -- Add autoreleasepool to release autorelease objects in advance when using GCD for 4.x #2475 - Fix resource key invalid when clean cached disk file #2463 - Fix the test case `testFLAnimatedImageViewSetImageWithURL` because of remote resource is not available #2450 - Add default `HTTP User-Agent` for specific system #2409 @@ -51,6 +49,14 @@ See [all tickets marked for the 4.4.3 release](https://github.com/SDWebImage/SDW - remove `addProgressCallback`, add `createDownloaderOperationWithUrl` #2336 - Fix the bug when `FLAnimatedImageView` firstly show one EXIF rotation JPEG `UIImage`, later animated GIF `FLAnimatedImage` will also be rotated #2406 - Replace `SDWebImageDownloaderOperation` with `NSOperation` to make generic #2397 +- Fix wrong image cache type when disk and memory cache missed #2529 +- Fix FLAnimatedImage version check issue for custom property `optimalFrameCacheSize` && `predrawingEnabled` #2543 + +#### Performances +- Add autoreleasepool to release autorelease objects in advance when using GCD for 4.x #2475 +- Optimize when scale = 1 #2520 + +#### Docs - Updated URLs after project was transfered to [SDWebImage organization](https://github.com/SDWebImage) #2510 f9d05d9 - Tidy up spacing for `README.md` #2511 - Remove versioneye from README #2424 diff --git a/README.md b/README.md index 18cda1a7..8af3434f 100644 --- a/README.md +++ b/README.md @@ -142,7 +142,16 @@ platform :ios, '7.0' pod 'SDWebImage', '~> 4.0' ``` -If you are using Swift, be sure to add `use_frameworks!` and set your target to iOS 8+: +##### Swift + +If you are using `Swift`, `Xcode 9+` and `CocoaPods` `1.5.0+`, you only need to set your target to `iOS 8+` if you need static library: + +``` +platform :ios, '8.0' +``` + +If not, you still need to add `use_frameworks!` to use dynamic framework: + ``` platform :ios, '8.0' use_frameworks! diff --git a/SDWebImage.podspec b/SDWebImage.podspec index 288bab62..c3abd417 100644 --- a/SDWebImage.podspec +++ b/SDWebImage.podspec @@ -24,6 +24,7 @@ Pod::Spec.new do |s| s.requires_arc = true s.framework = 'ImageIO' s.module_map = 'WebImage/SDWebImage.modulemap' + s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' } s.default_subspec = 'Core' diff --git a/SDWebImage.xcodeproj/project.pbxproj b/SDWebImage.xcodeproj/project.pbxproj index d6557325..6099f9b8 100644 --- a/SDWebImage.xcodeproj/project.pbxproj +++ b/SDWebImage.xcodeproj/project.pbxproj @@ -111,6 +111,10 @@ 32D1222C2080B2EB003685A3 /* SDImageCachesManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D1221C2080B2EB003685A3 /* SDImageCachesManager.m */; }; 32D122302080B2EB003685A3 /* SDImageCachesManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D1221D2080B2EB003685A3 /* SDImageCachesManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; 32D122322080B2EB003685A3 /* SDImageCachesManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D1221D2080B2EB003685A3 /* SDImageCachesManager.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 32D3CDCE21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D3CDCC21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m */; }; + 32D3CDCF21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m in Sources */ = {isa = PBXBuildFile; fileRef = 32D3CDCC21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m */; }; + 32D3CDD021DDE87300C4DB49 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D3CDCD21DDE87300C4DB49 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 32D3CDD121DDE87300C4DB49 /* UIImage+MemoryCacheCost.h in Headers */ = {isa = PBXBuildFile; fileRef = 32D3CDCD21DDE87300C4DB49 /* UIImage+MemoryCacheCost.h */; settings = {ATTRIBUTES = (Public, ); }; }; 32EB6D8E206D132E005CAEF6 /* SDAnimatedImageRep.m in Sources */ = {isa = PBXBuildFile; fileRef = 320224BA203979BA00E9F285 /* SDAnimatedImageRep.m */; }; 32EB6D91206D132E005CAEF6 /* SDAnimatedImageRep.m in Sources */ = {isa = PBXBuildFile; fileRef = 320224BA203979BA00E9F285 /* SDAnimatedImageRep.m */; }; 32F21B5120788D8C0036B1D5 /* SDWebImageDownloaderRequestModifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 32F21B4F20788D8C0036B1D5 /* SDWebImageDownloaderRequestModifier.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -275,6 +279,8 @@ 32D1221B2080B2EB003685A3 /* SDImageCacheDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDImageCacheDefine.m; sourceTree = ""; }; 32D1221C2080B2EB003685A3 /* SDImageCachesManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDImageCachesManager.m; sourceTree = ""; }; 32D1221D2080B2EB003685A3 /* SDImageCachesManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDImageCachesManager.h; sourceTree = ""; }; + 32D3CDCC21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+MemoryCacheCost.m"; sourceTree = ""; }; + 32D3CDCD21DDE87300C4DB49 /* UIImage+MemoryCacheCost.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+MemoryCacheCost.h"; sourceTree = ""; }; 32F21B4F20788D8C0036B1D5 /* SDWebImageDownloaderRequestModifier.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDWebImageDownloaderRequestModifier.h; sourceTree = ""; }; 32F21B5020788D8C0036B1D5 /* SDWebImageDownloaderRequestModifier.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SDWebImageDownloaderRequestModifier.m; sourceTree = ""; }; 32F7C06D2030114C00873181 /* SDImageTransformer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SDImageTransformer.h; sourceTree = ""; }; @@ -544,6 +550,8 @@ 321E60BD1F38E91700405457 /* UIImage+ForceDecode.m */, 32F7C07D2030719600873181 /* UIImage+Transform.h */, 32F7C07C2030719600873181 /* UIImage+Transform.m */, + 32D3CDCD21DDE87300C4DB49 /* UIImage+MemoryCacheCost.h */, + 32D3CDCC21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m */, 4397D2F41D0DE2DF00BB2784 /* NSImage+Compatibility.h */, 4397D2F51D0DE2DF00BB2784 /* NSImage+Compatibility.m */, AB615301192DA24600A2D8E9 /* UIView+WebCacheOperation.h */, @@ -628,6 +636,7 @@ 32D122202080B2EB003685A3 /* SDImageCacheDefine.h in Headers */, 32B9B539206ED4230026769D /* SDWebImageDownloaderConfig.h in Headers */, 3257EAFA21898AED0097B271 /* SDImageGraphics.h in Headers */, + 32D3CDD121DDE87300C4DB49 /* UIImage+MemoryCacheCost.h in Headers */, 328BB6AC2081FEE500760D6C /* SDWebImageCacheSerializer.h in Headers */, 321B378F2083290E00C0EA77 /* SDImageLoadersManager.h in Headers */, 329A185B1FFF5DFD008C9A2F /* UIImage+Metadata.h in Headers */, @@ -684,6 +693,7 @@ 32CF1C071FA496B000004BD1 /* SDImageCoderHelper.h in Headers */, 32F7C0842030719600873181 /* UIImage+Transform.h in Headers */, 3257EAF921898AED0097B271 /* SDImageGraphics.h in Headers */, + 32D3CDD021DDE87300C4DB49 /* UIImage+MemoryCacheCost.h in Headers */, 53761316155AD0D5005750A4 /* SDImageCache.h in Headers */, 325312C8200F09910046BF1E /* SDWebImageTransition.h in Headers */, 32C0FDE12013426C001B8F2D /* SDWebImageIndicator.h in Headers */, @@ -879,6 +889,7 @@ 328BB6A42081FED200760D6C /* SDWebImageCacheKeyFilter.m in Sources */, 4A2CAE2E1AB4BB7500B6BC39 /* UIImage+GIF.m in Sources */, 80B6DF822142B44400BCB334 /* NSButton+WebCache.m in Sources */, + 32D3CDCF21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m in Sources */, 320CAE1D2086F50500CFFC80 /* SDWebImageError.m in Sources */, 32CF1C0F1FA496B000004BD1 /* SDImageCoderHelper.m in Sources */, 328BB6D52082581100760D6C /* SDMemoryCache.m in Sources */, @@ -933,6 +944,7 @@ 328BB6A22081FED200760D6C /* SDWebImageCacheKeyFilter.m in Sources */, 53761309155AD0D5005750A4 /* SDImageCache.m in Sources */, 80B6DF832142B44500BCB334 /* NSButton+WebCache.m in Sources */, + 32D3CDCE21DDE87300C4DB49 /* UIImage+MemoryCacheCost.m in Sources */, 320CAE1B2086F50500CFFC80 /* SDWebImageError.m in Sources */, 32CF1C0D1FA496B000004BD1 /* SDImageCoderHelper.m in Sources */, 328BB6D32082581100760D6C /* SDMemoryCache.m in Sources */, diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 4007ce1d..e650a257 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -14,6 +14,7 @@ #import "SDImageTransformer.h" #import "SDImageCoderHelper.h" #import "SDAnimatedImage.h" +#import "UIImage+MemoryCacheCost.h" @interface SDImageCache () @@ -178,7 +179,7 @@ } // if memory cache is enabled if (toMemory && self.config.shouldCacheImagesInMemory) { - NSUInteger cost = SDMemoryCacheCostForImage(image); + NSUInteger cost = image.sd_memoryCost; [self.memCache setObject:image forKey:key cost:cost]; } @@ -216,7 +217,7 @@ if (!image || !key) { return; } - NSUInteger cost = SDMemoryCacheCostForImage(image); + NSUInteger cost = image.sd_memoryCost; [self.memCache setObject:image forKey:key cost:cost]; } @@ -294,7 +295,7 @@ - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key { UIImage *diskImage = [self diskImageForKey:key]; if (diskImage && self.config.shouldCacheImagesInMemory) { - NSUInteger cost = SDMemoryCacheCostForImage(diskImage); + NSUInteger cost = diskImage.sd_memoryCost; [self.memCache setObject:diskImage forKey:key cost:cost]; } @@ -411,7 +412,7 @@ // decode image data only if in-memory cache missed diskImage = [self diskImageForKey:key data:diskData options:options context:context]; if (diskImage && self.config.shouldCacheImagesInMemory) { - NSUInteger cost = SDMemoryCacheCostForImage(diskImage); + NSUInteger cost = diskImage.sd_memoryCost; [self.memCache setObject:diskImage forKey:key cost:cost]; } } diff --git a/SDWebImage/SDMemoryCache.h b/SDWebImage/SDMemoryCache.h index 0fb69150..48c0c5f2 100644 --- a/SDWebImage/SDMemoryCache.h +++ b/SDWebImage/SDMemoryCache.h @@ -8,14 +8,6 @@ #import "SDWebImageCompat.h" -/** - Return the memory cache cost for specify image - - @param image The image to store in cache - @return The memory cost for the image - */ -FOUNDATION_EXPORT NSUInteger SDMemoryCacheCostForImage(UIImage * _Nullable image); - @class SDImageCacheConfig; // A protocol to allow custom memory cache used in SDImageCache. @protocol SDMemoryCache diff --git a/SDWebImage/SDMemoryCache.m b/SDWebImage/SDMemoryCache.m index d99ac764..8a03f117 100644 --- a/SDWebImage/SDMemoryCache.m +++ b/SDWebImage/SDMemoryCache.m @@ -8,14 +8,7 @@ #import "SDMemoryCache.h" #import "SDImageCacheConfig.h" - -NSUInteger SDMemoryCacheCostForImage(UIImage * _Nullable image) { -#if SD_MAC - return image.size.height * image.size.width; -#elif SD_UIKIT || SD_WATCH - return image.size.height * image.size.width * image.scale * image.scale; -#endif -} +#import "UIImage+MemoryCacheCost.h" static void * SDMemoryCacheContext = &SDMemoryCacheContext; @@ -109,7 +102,7 @@ static void * SDMemoryCacheContext = &SDMemoryCacheContext; // Sync cache NSUInteger cost = 0; if ([obj isKindOfClass:[UIImage class]]) { - cost = SDMemoryCacheCostForImage(obj); + cost = [(UIImage *)obj sd_memoryCost]; } [super setObject:obj forKey:key cost:cost]; } diff --git a/SDWebImage/UIImage+MemoryCacheCost.h b/SDWebImage/UIImage+MemoryCacheCost.h new file mode 100644 index 00000000..9ecf5b94 --- /dev/null +++ b/SDWebImage/UIImage+MemoryCacheCost.h @@ -0,0 +1,23 @@ +/* + * This file is part of the SDWebImage package. + * (c) Olivier Poitrey + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +#import "SDWebImageCompat.h" + +@interface UIImage (MemoryCacheCost) + +/** + The memory cache cost for specify image used by image cache. The cost function is the pixles count held in memory. + If you set some associated object to `UIImage`, you can set the custom value to indicate the memory cost. + + For `UIImage`, this method return the single frame pixles count when `image.images` is nil for static image. Retuen full frame pixels count when `image.images` is not nil for animated image. + For `NSImage`, this method return the single frame pixels count because `NSImage` does not store all frames in memory. + @note Note that because of the limitations of categories this property can get out of sync if you create another instance with CGImage or other methods. + */ +@property (assign, nonatomic) NSUInteger sd_memoryCost; + +@end diff --git a/SDWebImage/UIImage+MemoryCacheCost.m b/SDWebImage/UIImage+MemoryCacheCost.m new file mode 100644 index 00000000..6f1375d7 --- /dev/null +++ b/SDWebImage/UIImage+MemoryCacheCost.m @@ -0,0 +1,38 @@ +/* + * This file is part of the SDWebImage package. + * (c) Olivier Poitrey + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +#import "UIImage+MemoryCacheCost.h" +#import "objc/runtime.h" + +FOUNDATION_STATIC_INLINE NSUInteger SDMemoryCacheCostForImage(UIImage *image) { +#if SD_MAC + return image.size.height * image.size.width; +#elif SD_UIKIT || SD_WATCH + NSUInteger imageSize = image.size.height * image.size.width * image.scale * image.scale; + return image.images ? (imageSize * image.images.count) : imageSize; +#endif +} + +@implementation UIImage (MemoryCacheCost) + +- (NSUInteger)sd_memoryCost { + NSNumber *value = objc_getAssociatedObject(self, @selector(sd_memoryCost)); + NSUInteger memoryCost; + if (value != nil) { + memoryCost = [value unsignedIntegerValue]; + } else { + memoryCost = SDMemoryCacheCostForImage(self); + } + return memoryCost; +} + +- (void)setSd_memoryCost:(NSUInteger)sd_memoryCost { + objc_setAssociatedObject(self, @selector(sd_memoryCost), @(sd_memoryCost), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +@end diff --git a/WebImage/SDWebImage.h b/WebImage/SDWebImage.h index 49eeae80..b09d2d1d 100644 --- a/WebImage/SDWebImage.h +++ b/WebImage/SDWebImage.h @@ -43,6 +43,7 @@ FOUNDATION_EXPORT const unsigned char WebImageVersionString[]; #import #import #import +#import #import #import #import