Skip to content
This repository was archived by the owner on Aug 3, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion Example/SDWebImageFLPlugin/SDViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 6 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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];
```

Expand All @@ -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)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,7 @@

#import <SDWebImage/SDWebImage.h>
#import <FLAnimatedImage/FLAnimatedImage.h>
#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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -57,18 +54,21 @@ - (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
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;
}
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

#import <FLAnimatedImage/FLAnimatedImage.h>
#import <SDWebImage/SDWebImage.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;

/**
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 <SDAnimatedImage>

/**
The `FLAnimatedImage` instance for GIF representation
*/
@property (nonatomic, strong, nullable, 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;


// 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
127 changes: 127 additions & 0 deletions SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

#import "SDFLAnimatedImage.h"

SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize = @"optimalFrameCacheSize";
SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled = @"predrawingEnabled";

@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 {
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<SDAnimatedImageCoder>)animatedCoder scale:(CGFloat)scale {
// Does not support progressive load for GIF images at all
return nil;
}

#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 self;
}
FLAnimatedImage *animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
if (!animatedImage) {
return self;
}
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 - SDAnimatedImageProvider

- (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

This file was deleted.

Loading