-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from max-prokopenko/refactor/sdwebimage
build: v3.0.0
- Loading branch information
Showing
6 changed files
with
74 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,40 @@ | ||
// #import <Foundation/Foundation.h> | ||
// #import <React/RCTViewManager.h> | ||
|
||
// #import "UIView+React.h" | ||
#import "FLAnimatedImage.h" | ||
#import "GifView.h" | ||
#import <React/RCTLog.h> | ||
#import <SDWebImage/SDWebImage.h> | ||
|
||
@implementation GifView { | ||
|
||
FLAnimatedImageView *_imageView; | ||
FLAnimatedImage *_image; | ||
|
||
SDAnimatedImageView *_imageView; | ||
} | ||
|
||
- (instancetype)initWithFrame:(CGRect)frame | ||
{ | ||
if ((self = [super initWithFrame:frame])) { | ||
_imageView = [[FLAnimatedImageView alloc] init]; | ||
_imageView = [SDAnimatedImageView new]; | ||
|
||
_imageView.sd_imageTransition = SDWebImageTransition.fadeTransition; | ||
_imageView.shouldIncrementalLoad = NO; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)layoutSubviews | ||
{ | ||
_imageView.frame = self.bounds; | ||
[self addSubview:_imageView]; | ||
_imageView.frame = self.bounds; | ||
|
||
[self addSubview:_imageView]; | ||
} | ||
|
||
- (void)setSource:(NSString *)source | ||
{ | ||
if (![source isEqual:_source]) { | ||
_source = [source copy]; | ||
[self reloadImage]; | ||
NSURL *url = [NSURL URLWithString:_source]; | ||
|
||
CGSize thumbnailSize = CGSizeMake(self.frame.size.width, self.frame.size.height); | ||
|
||
[_imageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageProgressiveLoad context:@{SDWebImageContextImageThumbnailPixelSize : @(thumbnailSize)}]; | ||
|
||
} | ||
} | ||
- (void)setResizeMode:(NSString *)resizeMode | ||
{ | ||
if(![resizeMode isEqual:_resizeMode]) { | ||
_resizeMode = resizeMode; | ||
[self reloadImage]; | ||
} | ||
} | ||
|
||
-(void)reloadImage { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
NSURL *url = [NSURL URLWithString:self->_source]; | ||
[self loadAnimatedImageWithURL:url completion:^(FLAnimatedImage *animatedImage) { | ||
self->_image = animatedImage; | ||
NSInteger cMode = UIViewContentModeScaleAspectFit; | ||
if([self->_resizeMode isEqualToString:@"contain"]) { | ||
cMode = UIViewContentModeScaleAspectFit; | ||
} else if ([self->_resizeMode isEqualToString:@"cover"]) { | ||
cMode = UIViewContentModeScaleAspectFill; | ||
} | ||
self->_imageView.contentMode = cMode; | ||
self->_imageView.animatedImage = self->_image; | ||
if(self->_paused) { | ||
[self->_imageView stopAnimating]; | ||
} | ||
if (self->_onLoadEnd) { | ||
self->_onLoadEnd(@{}); | ||
} | ||
}]; | ||
}); | ||
} | ||
|
||
- (void)loadAnimatedImageWithURL:(NSURL *const)url completion:(void (^)(FLAnimatedImage *animatedImage))completion | ||
{ | ||
NSArray *const pathComponents = url.pathComponents; | ||
NSString *filename = [pathComponents componentsJoinedByString:@"_"]; | ||
NSString *const diskPath = [NSTemporaryDirectory() stringByAppendingPathComponent:filename]; | ||
NSData * __block animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath]; | ||
FLAnimatedImage * __block animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData]; | ||
if (animatedImage) { | ||
if (completion) { | ||
completion(animatedImage); | ||
} | ||
} else { | ||
[[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | ||
animatedImageData = data; | ||
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData]; | ||
if (animatedImage) { | ||
if (completion) { | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
completion(animatedImage); | ||
}); | ||
} | ||
[data writeToFile:diskPath atomically:YES]; | ||
} | ||
}] resume]; | ||
} | ||
} | ||
|
||
- (void) setPaused:(BOOL *)paused { | ||
_paused = paused; | ||
if(paused) { | ||
[_imageView stopAnimating]; | ||
|
||
} else { | ||
[_imageView startAnimating]; | ||
} | ||
} | ||
|
||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters