2015-03-23 22:07:33 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
#import "RCTNetworkImageView.h"
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
#import "RCTAssert.h"
|
2015-03-06 17:54:10 +00:00
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTGIFImage.h"
|
2015-01-30 01:10:49 +00:00
|
|
|
#import "RCTImageDownloader.h"
|
|
|
|
#import "RCTUtils.h"
|
2015-05-22 14:17:08 +00:00
|
|
|
#import "UIView+React.h"
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
@implementation RCTNetworkImageView
|
|
|
|
{
|
|
|
|
BOOL _deferred;
|
|
|
|
NSURL *_imageURL;
|
|
|
|
NSURL *_deferredImageURL;
|
|
|
|
NSUInteger _deferSentinel;
|
|
|
|
RCTImageDownloader *_imageDownloader;
|
|
|
|
id _downloadToken;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (instancetype)initWithImageDownloader:(RCTImageDownloader *)imageDownloader
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTAssertParam(imageDownloader);
|
|
|
|
|
|
|
|
if ((self = [super initWithFrame:CGRectZero])) {
|
2015-01-30 01:10:49 +00:00
|
|
|
_deferSentinel = 0;
|
|
|
|
_imageDownloader = imageDownloader;
|
|
|
|
self.userInteractionEnabled = NO;
|
2015-06-15 14:53:45 +00:00
|
|
|
self.contentMode = UIViewContentModeScaleAspectFill;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(-initWithFrame:(CGRect)frame)
|
|
|
|
RCT_NOT_IMPLEMENTED(-initWithCoder:(NSCoder *)aDecoder)
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (NSURL *)imageURL
|
|
|
|
{
|
2015-05-22 14:17:08 +00:00
|
|
|
// We clear our imageURL when we are not in a window for a while,
|
2015-01-30 01:10:49 +00:00
|
|
|
// to make sure we don't consume network resources while offscreen.
|
|
|
|
// However we don't want to expose this hackery externally.
|
|
|
|
return _deferred ? _deferredImageURL : _imageURL;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:17:08 +00:00
|
|
|
- (void)setBackgroundColor:(UIColor *)backgroundColor
|
|
|
|
{
|
|
|
|
super.backgroundColor = backgroundColor;
|
|
|
|
[self _updateImage];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reactSetFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
[super reactSetFrame:frame];
|
|
|
|
[self _updateImage];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_updateImage
|
|
|
|
{
|
|
|
|
[self setImageURL:_imageURL resetToDefaultImageWhileLoading:NO];
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
- (void)setImageURL:(NSURL *)imageURL resetToDefaultImageWhileLoading:(BOOL)reset
|
|
|
|
{
|
2015-05-22 14:17:08 +00:00
|
|
|
if (![_imageURL isEqual:imageURL] && _downloadToken) {
|
|
|
|
[_imageDownloader cancelDownload:_downloadToken];
|
|
|
|
_downloadToken = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
_imageURL = imageURL;
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
if (_deferred) {
|
|
|
|
_deferredImageURL = imageURL;
|
|
|
|
} else {
|
2015-05-22 14:17:08 +00:00
|
|
|
if (!imageURL) {
|
|
|
|
self.layer.contents = nil;
|
|
|
|
return;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
if (reset) {
|
|
|
|
self.layer.contentsScale = _defaultImage.scale;
|
|
|
|
self.layer.contents = (__bridge id)_defaultImage.CGImage;
|
2015-03-02 18:42:31 +00:00
|
|
|
self.layer.minificationFilter = kCAFilterTrilinear;
|
|
|
|
self.layer.magnificationFilter = kCAFilterTrilinear;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
2015-02-04 00:15:20 +00:00
|
|
|
if ([imageURL.pathExtension caseInsensitiveCompare:@"gif"] == NSOrderedSame) {
|
|
|
|
_downloadToken = [_imageDownloader downloadDataForURL:imageURL block:^(NSData *data, NSError *error) {
|
|
|
|
if (data) {
|
2015-03-31 03:12:32 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-05-22 14:17:08 +00:00
|
|
|
if (imageURL != self.imageURL) {
|
|
|
|
// Image has changed
|
|
|
|
return;
|
|
|
|
}
|
2015-03-31 03:12:32 +00:00
|
|
|
CAKeyframeAnimation *animation = RCTGIFImageWithData(data);
|
|
|
|
self.layer.contentsScale = 1.0;
|
|
|
|
self.layer.minificationFilter = kCAFilterLinear;
|
|
|
|
self.layer.magnificationFilter = kCAFilterLinear;
|
|
|
|
[self.layer addAnimation:animation forKey:@"contents"];
|
|
|
|
});
|
2015-05-22 14:17:08 +00:00
|
|
|
} else if (error) {
|
|
|
|
RCTLogWarn(@"Unable to download image data. Error: %@", error);
|
2015-02-04 00:15:20 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
} else {
|
2015-05-22 14:17:08 +00:00
|
|
|
_downloadToken = [_imageDownloader downloadImageForURL:imageURL size:self.bounds.size scale:RCTScreenScale() resizeMode:self.contentMode backgroundColor:self.backgroundColor block:^(UIImage *image, NSError *error) {
|
2015-02-04 00:15:20 +00:00
|
|
|
if (image) {
|
2015-03-31 03:12:32 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-05-22 14:17:08 +00:00
|
|
|
if (imageURL != self.imageURL) {
|
|
|
|
// Image has changed
|
|
|
|
return;
|
|
|
|
}
|
2015-03-31 03:12:32 +00:00
|
|
|
[self.layer removeAnimationForKey:@"contents"];
|
|
|
|
self.layer.contentsScale = image.scale;
|
|
|
|
self.layer.contents = (__bridge id)image.CGImage;
|
|
|
|
});
|
2015-05-22 14:17:08 +00:00
|
|
|
} else if (error) {
|
|
|
|
RCTLogWarn(@"Unable to download image. Error: %@", error);
|
2015-02-04 00:15:20 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setImageURL:(NSURL *)imageURL
|
|
|
|
{
|
|
|
|
[self setImageURL:imageURL resetToDefaultImageWhileLoading:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)willMoveToWindow:(UIWindow *)newWindow
|
|
|
|
{
|
|
|
|
[super willMoveToWindow:newWindow];
|
|
|
|
if (newWindow != nil && _deferredImageURL) {
|
|
|
|
// Immediately exit deferred mode and restore the imageURL that we saved when we went offscreen.
|
|
|
|
[self setImageURL:_deferredImageURL resetToDefaultImageWhileLoading:YES];
|
|
|
|
_deferredImageURL = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_enterDeferredModeIfNeededForSentinel:(NSUInteger)sentinel
|
|
|
|
{
|
|
|
|
if (self.window == nil && _deferSentinel == sentinel) {
|
|
|
|
_deferred = YES;
|
|
|
|
[_imageDownloader cancelDownload:_downloadToken];
|
|
|
|
_downloadToken = nil;
|
|
|
|
_deferredImageURL = _imageURL;
|
|
|
|
_imageURL = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didMoveToWindow
|
|
|
|
{
|
|
|
|
[super didMoveToWindow];
|
|
|
|
if (self.window == nil) {
|
|
|
|
__weak RCTNetworkImageView *weakSelf = self;
|
|
|
|
NSUInteger sentinelAtDispatchTime = ++_deferSentinel;
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
|
|
|
|
[weakSelf _enterDeferredModeIfNeededForSentinel:sentinelAtDispatchTime];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|