From 03c3d45fd432c5d56ac66e6c83874ba17966c333 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 20 Aug 2018 13:29:15 +0800 Subject: [PATCH 1/3] Refactor the implementation to integrate FLAnimatedImage, create a custom animation class instead --- Example/SDWebImageFLPlugin/SDViewController.m | 1 - .../FLAnimatedImageView+WebCache.h | 16 +-- .../FLAnimatedImageView+WebCache.m | 10 +- .../FLAnimatedImageBridge/SDFLAnimatedImage.h | 33 ++++++ .../FLAnimatedImageBridge/SDFLAnimatedImage.m | 111 ++++++++++++++++++ .../FLAnimatedImageBridge/SDWebImageFLCoder.h | 18 --- .../FLAnimatedImageBridge/SDWebImageFLCoder.m | 75 ------------ .../UIImage+SDWebImageFLPlugin.h | 31 ----- .../UIImage+SDWebImageFLPlugin.m | 37 ------ .../Module/SDWebImageFLPlugin.h | 3 +- 10 files changed, 151 insertions(+), 184 deletions(-) create mode 100644 SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h create mode 100644 SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m delete mode 100644 SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.h delete mode 100644 SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.m delete mode 100644 SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.h delete mode 100644 SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.m diff --git a/Example/SDWebImageFLPlugin/SDViewController.m b/Example/SDWebImageFLPlugin/SDViewController.m index bece6d1..9eb2dc1 100644 --- a/Example/SDWebImageFLPlugin/SDViewController.m +++ b/Example/SDWebImageFLPlugin/SDViewController.m @@ -17,7 +17,6 @@ @implementation SDViewController - (void)viewDidLoad { [super viewDidLoad]; - [[SDImageCodersManager sharedManager] addCoder:[SDWebImageFLCoder sharedCoder]]; // Do any additional setup after loading the view, typically from a nib. diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.h b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.h index ac25752..a139eaf 100644 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.h +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.h @@ -8,21 +8,7 @@ #import #import -#import "UIImage+SDWebImageFLPlugin.h" - -/** - * Optimal frame cache size of FLAnimatedImage during initializer. (1.0.11 version later) - * This value will help you set `optimalFrameCacheSize` arg of FLAnimatedImage initializer after image load. - * Defaults to 0. - */ -FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize; -/** - * Predrawing control of FLAnimatedImage during initializer. (1.0.11 version later) - * This value will help you set `predrawingEnabled` arg of FLAnimatedImage initializer after image load. - * Defaults to YES. - */ -FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled; - +#import "SDFLAnimatedImage.h" /** * A category for the FLAnimatedImage imageView class that hooks it to the SDWebImage system. diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m index c30ce53..a6edc85 100644 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m @@ -8,9 +8,6 @@ #import "FLAnimatedImageView+WebCache.h" -SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize = @"optimalFrameCacheSize"; -SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled = @"predrawingEnabled"; - @implementation FLAnimatedImageView (WebCache) - (void)sd_setImageWithURL:(nullable NSURL *)url { @@ -57,7 +54,7 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url } else { mutableContext = [NSMutableDictionary dictionary]; } - mutableContext[SDWebImageContextSetImageOperationKey] = NSStringFromClass(self.class); + mutableContext[SDWebImageContextAnimatedImageClass] = [SDFLAnimatedImage class]; __weak typeof(self)weakSelf = self; [self sd_internalSetImageWithURL:url placeholderImage:placeholder @@ -68,7 +65,10 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url if (!strongSelf) { return; } - FLAnimatedImage *animatedImage = image.sd_FLAnimatedImage; + FLAnimatedImage *animatedImage; + if ([image isKindOfClass:[SDFLAnimatedImage class]]) { + animatedImage = ((SDFLAnimatedImage *)image).animatedImage; + } if (animatedImage) { // FLAnimatedImage framework contains a bug that cause GIF been rotated if previous rendered image orientation is not Up. We have to call `setImage:` with non-nil image to reset the state. See `https://github.com/rs/SDWebImage/issues/2402` strongSelf.image = animatedImage.posterImage; diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h new file mode 100644 index 0000000..806bc5b --- /dev/null +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h @@ -0,0 +1,33 @@ +/* + * 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 +#import + +/** + A wrapper class to allow `FLAnimatedImage` to be compatible for SDWebImage loading/cache/rendering system. The `GIF` image loading from `FLAnimatedImageView+WebCache` category, will use this subclass instead of `UIImage`. + + @note Though this class conforms to `SDAnimatedImage` protocol, so it's compatible to be used for `SDAnimatedImageView`. But it's normally discouraged to do so. Because it does not provide optimization for animation rendering. Instead, use `SDAnimatedImage` class with `SDAnimatedImageView`. + */ +@interface SDFLAnimatedImage : UIImage + +/** + The `FLAnimatedImage` instance for GIF representation + */ +@property (nonatomic, strong, nonnull, readonly) FLAnimatedImage *animatedImage; + +/** + Create the wrapper with specify `FLAnimatedImage` instance. The instance should be nonnull. + This is a convenience method for some use cases, for example, create a placeholder with `FLAnimatedImage`. + + @param animatedImage The `FLAnimatedImage` instance + @return An initialized object + */ +- (nonnull instancetype)initWithAnimatedImage:(nonnull FLAnimatedImage *)animatedImage; + +@end diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m new file mode 100644 index 0000000..e6d8859 --- /dev/null +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m @@ -0,0 +1,111 @@ +/* + * 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 "SDFLAnimatedImage.h" + +@interface SDFLAnimatedImage () + +@property (nonatomic, strong, nullable) FLAnimatedImage *animatedImage; + +@end + +@implementation SDFLAnimatedImage + +- (instancetype)initWithAnimatedImage:(FLAnimatedImage *)animatedImage { + NSParameterAssert(animatedImage); + UIImage *posterImage = animatedImage.posterImage; + self = [super initWithCGImage:posterImage.CGImage scale:posterImage.scale orientation:posterImage.imageOrientation]; + if (self) { + self.animatedImage = animatedImage; + } + return self; +} + ++ (instancetype)imageWithContentsOfFile:(NSString *)path { + return [[self alloc] initWithContentsOfFile:path]; +} + ++ (instancetype)imageWithData:(NSData *)data { + return [[self alloc] initWithData:data]; +} + ++ (instancetype)imageWithData:(NSData *)data scale:(CGFloat)scale { + return [[self alloc] initWithData:data scale:scale]; +} + +- (instancetype)initWithData:(NSData *)data { + return [self initWithData:data scale:1]; +} + +- (instancetype)initWithContentsOfFile:(NSString *)path { + NSData *data = [NSData dataWithContentsOfFile:path]; + return [self initWithData:data]; +} + +- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale { + FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data]; + if (!animatedImage) { + return nil; + } + return [self initWithAnimatedImage:animatedImage]; +} + +#pragma mark - NSSecureCoding + +- (instancetype)initWithCoder:(NSCoder *)aDecoder { + self = [super initWithCoder:aDecoder]; + if (self) { + NSData *animatedImageData = [aDecoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(animatedImageData))]; + if (!animatedImageData) { + return nil; + } + FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData]; + if (!animatedImage) { + return nil; + } + self.animatedImage = animatedImage; + } + return self; +} + +- (void)encodeWithCoder:(NSCoder *)aCoder { + [super encodeWithCoder:aCoder]; + NSData *animatedImageData = self.animatedImageData; + if (animatedImageData) { + [aCoder encodeObject:animatedImageData forKey:NSStringFromSelector(@selector(animatedImageData))]; + } +} + +#pragma mark - SDAnimatedImage + +- (instancetype)initWithAnimatedCoder:(id)animatedCoder scale:(CGFloat)scale { + // Does not support progressive load for GIF images at all + return nil; +} + +- (nullable NSData *)animatedImageData { + return self.animatedImage.data; +} + +- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index { + return [self.animatedImage.delayTimesForIndexes[@(index)] doubleValue]; +} + +- (nullable UIImage *)animatedImageFrameAtIndex:(NSUInteger)index { + return [self.animatedImage imageLazilyCachedAtIndex:index]; +} + +- (NSUInteger)animatedImageFrameCount { + return self.animatedImage.frameCount; +} + +- (NSUInteger)animatedImageLoopCount { + return self.animatedImage.loopCount; +} + +@end diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.h b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.h deleted file mode 100644 index d3badb4..0000000 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * 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 - -// A coder which decode the GIF image, into `FLAnimatedImage` representation and bind the associated object. See `UIImage+SDWebImageFLPlugin` for more detailed information. -// When you want to use `FLAnimatedImageView` to load image, be sure to add this coder before `SDImageGIFCoder`, to ensure this coder been processed before `SDImageGIFCoder` - -@interface SDWebImageFLCoder : NSObject - -@property (nonatomic, class, readonly, nonnull) SDWebImageFLCoder *sharedCoder; - -@end diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.m b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.m deleted file mode 100644 index aeb5573..0000000 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDWebImageFLCoder.m +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 "SDWebImageFLCoder.h" -#import "FLAnimatedImageView+WebCache.h" - -@implementation SDWebImageFLCoder - -+ (SDWebImageFLCoder *)sharedCoder { - static SDWebImageFLCoder *coder; - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - coder = [[SDWebImageFLCoder alloc] init]; - }); - return coder; -} - -- (BOOL)canDecodeFromData:(NSData *)data { - return ([NSData sd_imageFormatForImageData:data] == SDImageFormatGIF); -} - -- (UIImage *)decodedImageWithData:(NSData *)data options:(SDImageCoderOptions *)options { - SDWebImageContext *context = options[SDImageCoderWebImageContext]; - NSString *operationKey = context[SDWebImageContextSetImageOperationKey]; - Class imageViewClass = NSClassFromString(operationKey); - - // Check if image request come from `FLAnimatedImageView` - if (imageViewClass && [imageViewClass isSubclassOfClass:FLAnimatedImageView.class]) { - // Parse args - BOOL predrawingEnabled = YES; - if (context[SDWebImageContextPredrawingEnabled]) { - predrawingEnabled = [context[SDWebImageContextPredrawingEnabled] boolValue]; - } - NSUInteger optimalFrameCacheSize = 0; - if (context[SDWebImageContextOptimalFrameCacheSize]) { - optimalFrameCacheSize = [context[SDWebImageContextOptimalFrameCacheSize] unsignedIntegerValue]; - } - // Create FLAnimatedImage - FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data optimalFrameCacheSize:optimalFrameCacheSize predrawingEnabled:predrawingEnabled]; - if (!animatedImage) { - return nil; - } - - return [UIImage sd_imageWithFLAnimatedImage:animatedImage]; - } else { - UIImage *image; - NSArray> *coders = [SDImageCodersManager sharedManager].coders; - for (id coder in coders.reverseObjectEnumerator) { - if (coder == self) { - continue; - } - if ([coder canDecodeFromData:data]) { - image = [coder decodedImageWithData:data options:options]; - break; - } - } - - return image; - } -} - -- (BOOL)canEncodeToFormat:(SDImageFormat)format { - return NO; -} - -- (NSData *)encodedDataWithImage:(UIImage *)image format:(SDImageFormat)format options:(SDImageCoderOptions *)options { - return nil; -} - -@end diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.h b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.h deleted file mode 100644 index fa8c6d9..0000000 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 - -/** - * FLAnimatedImage is not a subclass of UIImage, so it's not possible to store it into the memory cache currently. However, for performance issue and cell reuse on FLAnimatedImageView, we use associate object to bind a FLAnimatedImage into UIImage when an animated GIF image load. For most cases, you don't need to touch this. - */ -@interface UIImage (SDWebImageFLPlugin) - -/** - * The FLAnimatedImage associated to the UIImage instance when an animated GIF image load. - * For most cases this is read-only and you should avoid manually setting this value. Util some cases like using placeholder with a `FLAnimatedImage`. - */ -@property (nonatomic, strong, nullable) FLAnimatedImage *sd_FLAnimatedImage; - -/** - Create a UIImage instance, which bind FLAnimatedImage using the `sd_FLAnimatedImage` on it. - This will use `posterImage` on FLAnimatedImage to create a new UIImage and specify the associate object. To avoid cycle retain. - - @param animatedImage FLAnimatedImage instance - @return The UIImage which bind FLAnimatedImage on it - */ -+ (nullable UIImage *)sd_imageWithFLAnimatedImage:(nullable FLAnimatedImage *)animatedImage; - -@end diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.m b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.m deleted file mode 100644 index d856f49..0000000 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/UIImage+SDWebImageFLPlugin.m +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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+SDWebImageFLPlugin.h" -#import -#import "objc/runtime.h" - -@implementation UIImage (SDWebImageFLPlugin) - -- (FLAnimatedImage *)sd_FLAnimatedImage { - return objc_getAssociatedObject(self, @selector(sd_FLAnimatedImage)); -} - -- (void)setSd_FLAnimatedImage:(FLAnimatedImage *)sd_FLAnimatedImage { - objc_setAssociatedObject(self, @selector(sd_FLAnimatedImage), sd_FLAnimatedImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC); -} - -+ (instancetype)sd_imageWithFLAnimatedImage:(FLAnimatedImage *)animatedImage { - UIImage *posterImage = animatedImage.posterImage; - CGImageRef imageRef = posterImage.CGImage; - if (!imageRef) { - return nil; - } - UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:posterImage.scale orientation:posterImage.imageOrientation]; - - image.sd_FLAnimatedImage = animatedImage; - image.sd_isDecoded = YES; // Avoid force decode and loss the associate object - - return image; -} - -@end diff --git a/SDWebImageFLPlugin/Module/SDWebImageFLPlugin.h b/SDWebImageFLPlugin/Module/SDWebImageFLPlugin.h index 2d441e7..a486a59 100644 --- a/SDWebImageFLPlugin/Module/SDWebImageFLPlugin.h +++ b/SDWebImageFLPlugin/Module/SDWebImageFLPlugin.h @@ -11,8 +11,7 @@ #endif #import -#import -#import +#import FOUNDATION_EXPORT double SDWebImageFLPluginVersionNumber; From c3cf593e820cc4558b04fecc285cf4d686af7f59 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 20 Aug 2018 13:40:34 +0800 Subject: [PATCH 2/3] Update the readme, about the new way of use FLAnimatedImage plugin --- README.md | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c24a0a5..e8d95af 100644 --- a/README.md +++ b/README.md @@ -15,23 +15,7 @@ By using SDWebImageFLPlugin, you can use all you familiar SDWebImage's loading m To use it, simply make sure you use `FLAnimatedImageView` instead of `UIImageView` and import this plugin. ## Usage -**Important**: From v0.2.0, to load a network image on `FLAnimatedImageView`, at first you must ensure you added the `SDWebImageFLCodcer` to your coders manager (its priority should be higher than `SDImageGIFCoder`, or it will fallback to normal GIF rendering). You can add this on AppDelegate or somewhere earlier than usage, once is enough. - -+ Objective-C - -```objectivec -// The later added coders contains higher priority -[SDImageCodersManager.sharedManager addCoder:[SDWebImageFLCoder.sharedCoder]]; -``` - -+ Swift - -```swift -// The later added coders contains higher priority -SDImageCodersManager.shared.addCoder(SDWebImageFLCoder.shared) -``` - -Then, just simply call the View Category method like normal UIImageView. +To load images from network, just simply call the View Category method like normal UIImageView. + Objective-C @@ -47,14 +31,16 @@ let imageView: FLAnimatedImageView imageView.sd_setImage(with: URL(string: "http://www.domain.com/path/to/image.gif")) ``` -For placeholder, you can even provide a GIF image which use `FLAnimatedImage` instance (bind it on a `UIImage` instance), to allow GIF placeholder on `FLAnimatedImageView` +The magic because we create one custom animation class called `SDFLAnimatedImage` to load GIF images, and use `UIImage` for normal images. + +For placeholder, you can even provide a `FLAnimatedImage` to allow GIF placeholder on `FLAnimatedImageView` + Objective-C ```objectivec FLAnimatedImageView *imageView; FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:gifData]; -UIImage *placeholder = [UIImage sd_imageWithFLAnimatedImage:animatedImage]; +SDFLAnimatedImage *placeholder = [[SDFLAnimatedImage alloc] initWithAnimatedImage:animatedImage]; [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.gif"] placeholderImage:placeholder]; ``` @@ -63,7 +49,7 @@ UIImage *placeholder = [UIImage sd_imageWithFLAnimatedImage:animatedImage]; ```swift let imageView: FLAnimatedImageView let animatedImage = FLAnimatedImage(animatedGIFData: gifData) -let placeholder = UIImage.sd_image(with: animatedImage) +let placeholder = SDFLAnimatedImage(animatedImage: animatedImage) imageView.sd_setImage(with: URL(string: "http://www.domain.com/path/to/image.gif"), placeholderImage: placeholder) ``` From 74692f25d459ffbcc15dce207479171402505d38 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 21 Aug 2018 12:42:48 +0800 Subject: [PATCH 3/3] Update to the latest 5.x branch of SDWebImage Supports custom extra initializer args for FLAnimatedImage --- Example/Podfile | 2 +- Example/Podfile.lock | 11 +++++- .../FLAnimatedImageView+WebCache.m | 2 +- .../FLAnimatedImageBridge/SDFLAnimatedImage.h | 25 +++++++++++++- .../FLAnimatedImageBridge/SDFLAnimatedImage.m | 34 ++++++++++++++----- 5 files changed, 61 insertions(+), 13 deletions(-) diff --git a/Example/Podfile b/Example/Podfile index c946305..a8b7598 100644 --- a/Example/Podfile +++ b/Example/Podfile @@ -3,7 +3,7 @@ inhibit_all_warnings! target 'SDWebImageFLPlugin_Example' do pod 'SDWebImageFLPlugin', :path => '../' - #pod 'SDWebImage/Core', :git => 'https://github.com/rs/SDWebImage.git', :branch => '5.x' + pod 'SDWebImage/Core', :git => 'https://github.com/rs/SDWebImage.git', :branch => '5.x' target 'SDWebImageFLPlugin_Tests' do inherit! :search_paths diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 2a0ccbe..5a6625f 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -6,17 +6,26 @@ PODS: - SDWebImage/Core (>= 5.0.0-beta2) DEPENDENCIES: + - SDWebImage/Core (from `https://github.com/rs/SDWebImage.git`, branch `5.x`) - SDWebImageFLPlugin (from `../`) EXTERNAL SOURCES: + SDWebImage: + :branch: 5.x + :git: https://github.com/rs/SDWebImage.git SDWebImageFLPlugin: :path: ../ +CHECKOUT OPTIONS: + SDWebImage: + :commit: 2bb336bc12a5690dd11988c0bc8bef2c7318508d + :git: https://github.com/rs/SDWebImage.git + SPEC CHECKSUMS: FLAnimatedImage: 4a0b56255d9b05f18b6dd7ee06871be5d3b89e31 SDWebImage: 8e4aaf5805e954ad426702adf5a8c012a7f581d1 SDWebImageFLPlugin: 6661abba103bcdc5568d7921b2d46783f8bf5d86 -PODFILE CHECKSUM: 1f8ca120961078fc59296ae5aa436c8f3baf0e67 +PODFILE CHECKSUM: d45303c99b143f5fd7d828ef30667c31060a0484 COCOAPODS: 1.4.0 diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m index a6edc85..5efe534 100644 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/FLAnimatedImageView+WebCache.m @@ -60,7 +60,7 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url placeholderImage:placeholder options:options context:mutableContext - setImageBlock:^(UIImage *image, NSData *imageData) { + setImageBlock:^(UIImage * _Nullable image, NSData * _Nullable imageData, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { __strong typeof(weakSelf)strongSelf = weakSelf; if (!strongSelf) { return; diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h index 806bc5b..76a76d4 100644 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h @@ -9,6 +9,19 @@ #import #import +/** + * Optimal frame cache size of FLAnimatedImage during initializer. (1.0.11 version later) + * This value will help you set `optimalFrameCacheSize` arg of FLAnimatedImage initializer after image load. + * Defaults to 0. + */ +FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize; +/** + * Predrawing control of FLAnimatedImage during initializer. (1.0.11 version later) + * This value will help you set `predrawingEnabled` arg of FLAnimatedImage initializer after image load. + * Defaults to YES. + */ +FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled; + /** A wrapper class to allow `FLAnimatedImage` to be compatible for SDWebImage loading/cache/rendering system. The `GIF` image loading from `FLAnimatedImageView+WebCache` category, will use this subclass instead of `UIImage`. @@ -19,7 +32,7 @@ /** The `FLAnimatedImage` instance for GIF representation */ -@property (nonatomic, strong, nonnull, readonly) FLAnimatedImage *animatedImage; +@property (nonatomic, strong, nullable, readonly) FLAnimatedImage *animatedImage; /** Create the wrapper with specify `FLAnimatedImage` instance. The instance should be nonnull. @@ -30,4 +43,14 @@ */ - (nonnull instancetype)initWithAnimatedImage:(nonnull FLAnimatedImage *)animatedImage; + +// This class override these methods from UIImage, and it supports NSSecureCoding. +// You should use these methods to create a new animated image. Use other methods just call super instead. ++ (nullable instancetype)imageWithContentsOfFile:(nonnull NSString *)path; ++ (nullable instancetype)imageWithData:(nonnull NSData *)data; ++ (nullable instancetype)imageWithData:(nonnull NSData *)data scale:(CGFloat)scale; +- (nullable instancetype)initWithContentsOfFile:(nonnull NSString *)path; +- (nullable instancetype)initWithData:(nonnull NSData *)data; +- (nullable instancetype)initWithData:(nonnull NSData *)data scale:(CGFloat)scale; + @end diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m index e6d8859..58ac34f 100644 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m @@ -8,6 +8,9 @@ #import "SDFLAnimatedImage.h" +SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize = @"optimalFrameCacheSize"; +SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled = @"predrawingEnabled"; + @interface SDFLAnimatedImage () @property (nonatomic, strong, nullable) FLAnimatedImage *animatedImage; @@ -48,13 +51,31 @@ - (instancetype)initWithContentsOfFile:(NSString *)path { } - (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale { - FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data]; + return [self initWithData:data scale:scale options:nil]; +} + +- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale options:(SDImageCoderOptions *)options { + BOOL predrawingEnabled = YES; + SDWebImageContext *context = options[SDImageCoderWebImageContext]; + if (context[SDWebImageContextPredrawingEnabled]) { + predrawingEnabled = [context[SDWebImageContextPredrawingEnabled] boolValue]; + } + NSUInteger optimalFrameCacheSize = 0; + if (context[SDWebImageContextOptimalFrameCacheSize]) { + optimalFrameCacheSize = [context[SDWebImageContextOptimalFrameCacheSize] unsignedIntegerValue]; + } + FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:data optimalFrameCacheSize:optimalFrameCacheSize predrawingEnabled:predrawingEnabled]; if (!animatedImage) { return nil; } return [self initWithAnimatedImage:animatedImage]; } +- (instancetype)initWithAnimatedCoder:(id)animatedCoder scale:(CGFloat)scale { + // Does not support progressive load for GIF images at all + return nil; +} + #pragma mark - NSSecureCoding - (instancetype)initWithCoder:(NSCoder *)aDecoder { @@ -62,11 +83,11 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self) { NSData *animatedImageData = [aDecoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(animatedImageData))]; if (!animatedImageData) { - return nil; + return self; } FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData]; if (!animatedImage) { - return nil; + return self; } self.animatedImage = animatedImage; } @@ -81,12 +102,7 @@ - (void)encodeWithCoder:(NSCoder *)aCoder { } } -#pragma mark - SDAnimatedImage - -- (instancetype)initWithAnimatedCoder:(id)animatedCoder scale:(CGFloat)scale { - // Does not support progressive load for GIF images at all - return nil; -} +#pragma mark - SDAnimatedImageProvider - (nullable NSData *)animatedImageData { return self.animatedImage.data;