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 "RCTImageDownloader.h"
|
2015-02-19 01:48:13 +00:00
|
|
|
|
|
|
|
#import "RCTCache.h"
|
2015-05-22 14:17:08 +00:00
|
|
|
#import "RCTLog.h"
|
2015-01-30 01:10:49 +00:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-02-19 01:51:14 +00:00
|
|
|
typedef void (^RCTCachedDataDownloadBlock)(BOOL cached, NSData *data, NSError *error);
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
@implementation RCTImageDownloader
|
2015-02-19 01:48:13 +00:00
|
|
|
{
|
|
|
|
RCTCache *_cache;
|
2015-03-31 03:12:32 +00:00
|
|
|
dispatch_queue_t _processingQueue;
|
2015-02-19 01:51:14 +00:00
|
|
|
NSMutableDictionary *_pendingBlocks;
|
2015-02-19 01:48:13 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
+ (instancetype)sharedInstance
|
|
|
|
{
|
|
|
|
static RCTImageDownloader *sharedInstance;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedInstance = [[self alloc] init];
|
|
|
|
});
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:48:13 +00:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_cache = [[RCTCache alloc] initWithName:@"RCTImageDownloader"];
|
2015-03-31 03:12:32 +00:00
|
|
|
_processingQueue = dispatch_queue_create("com.facebook.React.DownloadProcessingQueue", DISPATCH_QUEUE_SERIAL);
|
|
|
|
_pendingBlocks = [[NSMutableDictionary alloc] init];
|
2015-02-19 01:48:13 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-03-31 03:12:32 +00:00
|
|
|
static NSString *RCTCacheKeyForURL(NSURL *url)
|
2015-02-19 01:48:13 +00:00
|
|
|
{
|
|
|
|
return url.absoluteString;
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:51:14 +00:00
|
|
|
- (id)_downloadDataForURL:(NSURL *)url block:(RCTCachedDataDownloadBlock)block
|
2015-02-19 01:48:13 +00:00
|
|
|
{
|
2015-03-31 03:12:32 +00:00
|
|
|
NSString *cacheKey = RCTCacheKeyForURL(url);
|
2015-02-19 01:48:13 +00:00
|
|
|
|
|
|
|
__block BOOL cancelled = NO;
|
|
|
|
__block NSURLSessionDataTask *task = nil;
|
2015-03-31 03:12:32 +00:00
|
|
|
|
2015-02-19 01:48:13 +00:00
|
|
|
dispatch_block_t cancel = ^{
|
2015-03-31 03:12:32 +00:00
|
|
|
|
2015-02-19 01:48:13 +00:00
|
|
|
cancelled = YES;
|
2015-02-19 01:51:14 +00:00
|
|
|
|
2015-03-31 03:12:32 +00:00
|
|
|
dispatch_async(_processingQueue, ^{
|
|
|
|
NSMutableArray *pendingBlocks = self->_pendingBlocks[cacheKey];
|
|
|
|
[pendingBlocks removeObject:block];
|
|
|
|
});
|
2015-02-19 01:51:14 +00:00
|
|
|
|
2015-02-19 01:48:13 +00:00
|
|
|
if (task) {
|
|
|
|
[task cancel];
|
|
|
|
task = nil;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-03-31 03:12:32 +00:00
|
|
|
dispatch_async(_processingQueue, ^{
|
|
|
|
NSMutableArray *pendingBlocks = _pendingBlocks[cacheKey];
|
|
|
|
if (pendingBlocks) {
|
|
|
|
[pendingBlocks addObject:block];
|
2015-02-19 01:51:14 +00:00
|
|
|
} else {
|
2015-03-31 03:12:32 +00:00
|
|
|
_pendingBlocks[cacheKey] = [NSMutableArray arrayWithObject:block];
|
|
|
|
|
|
|
|
__weak RCTImageDownloader *weakSelf = self;
|
|
|
|
RCTCachedDataDownloadBlock runBlocks = ^(BOOL cached, NSData *data, NSError *error) {
|
2015-04-08 01:26:09 +00:00
|
|
|
dispatch_async(_processingQueue, ^{
|
|
|
|
RCTImageDownloader *strongSelf = weakSelf;
|
|
|
|
NSArray *blocks = strongSelf->_pendingBlocks[cacheKey];
|
|
|
|
[strongSelf->_pendingBlocks removeObjectForKey:cacheKey];
|
2015-06-04 10:47:19 +00:00
|
|
|
for (RCTCachedDataDownloadBlock cacheDownloadBlock in blocks) {
|
|
|
|
cacheDownloadBlock(cached, data, error);
|
2015-04-08 01:26:09 +00:00
|
|
|
}
|
|
|
|
});
|
2015-03-31 03:12:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if ([_cache hasDataForKey:cacheKey]) {
|
|
|
|
[_cache fetchDataForKey:cacheKey completionHandler:^(NSData *data) {
|
|
|
|
if (!cancelled) {
|
|
|
|
runBlocks(YES, data, nil);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
|
|
|
|
if (!cancelled) {
|
|
|
|
runBlocks(NO, data, error);
|
|
|
|
}
|
|
|
|
}];
|
2015-02-19 01:51:14 +00:00
|
|
|
|
2015-03-31 03:12:32 +00:00
|
|
|
[task resume];
|
|
|
|
}
|
2015-02-19 01:51:14 +00:00
|
|
|
}
|
2015-03-31 03:12:32 +00:00
|
|
|
});
|
2015-02-19 01:48:13 +00:00
|
|
|
|
|
|
|
return [cancel copy];
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:51:14 +00:00
|
|
|
- (id)downloadDataForURL:(NSURL *)url block:(RCTDataDownloadBlock)block
|
2015-02-04 00:15:20 +00:00
|
|
|
{
|
2015-03-31 03:12:32 +00:00
|
|
|
NSString *cacheKey = RCTCacheKeyForURL(url);
|
2015-02-19 01:48:13 +00:00
|
|
|
__weak RCTImageDownloader *weakSelf = self;
|
2015-02-19 01:51:14 +00:00
|
|
|
return [self _downloadDataForURL:url block:^(BOOL cached, NSData *data, NSError *error) {
|
|
|
|
if (!cached) {
|
|
|
|
RCTImageDownloader *strongSelf = weakSelf;
|
|
|
|
[strongSelf->_cache setData:data forKey:cacheKey];
|
|
|
|
}
|
2015-03-31 03:12:32 +00:00
|
|
|
block(data, error);
|
2015-02-04 00:15:20 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:17:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the optimal context size for an image drawn using the clip rect
|
|
|
|
* returned by RCTClipRect.
|
|
|
|
*/
|
|
|
|
CGSize RCTTargetSizeForClipRect(CGRect);
|
|
|
|
CGSize RCTTargetSizeForClipRect(CGRect clipRect)
|
|
|
|
{
|
|
|
|
return (CGSize){
|
|
|
|
clipRect.size.width + clipRect.origin.x * 2,
|
|
|
|
clipRect.size.height + clipRect.origin.y * 2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function takes an input content size & scale (typically from an image),
|
|
|
|
* a target size & scale that it will be drawn into (typically a CGContext) and
|
|
|
|
* then calculates the optimal rectangle to draw the image into so that it will
|
|
|
|
* be sized and positioned correctly if drawn using the specified content mode.
|
|
|
|
*/
|
|
|
|
CGRect RCTClipRect(CGSize, CGFloat, CGSize, CGFloat, UIViewContentMode);
|
|
|
|
CGRect RCTClipRect(CGSize sourceSize, CGFloat sourceScale,
|
|
|
|
CGSize destSize, CGFloat destScale,
|
|
|
|
UIViewContentMode resizeMode)
|
|
|
|
{
|
|
|
|
// Precompensate for scale
|
|
|
|
CGFloat scale = sourceScale / destScale;
|
|
|
|
sourceSize.width *= scale;
|
|
|
|
sourceSize.height *= scale;
|
|
|
|
|
|
|
|
// Calculate aspect ratios if needed (don't bother is resizeMode == stretch)
|
|
|
|
CGFloat aspect = 0.0, targetAspect = 0.0;
|
|
|
|
if (resizeMode != UIViewContentModeScaleToFill) {
|
|
|
|
aspect = sourceSize.width / sourceSize.height;
|
|
|
|
targetAspect = destSize.width / destSize.height;
|
|
|
|
if (aspect == targetAspect) {
|
|
|
|
resizeMode = UIViewContentModeScaleToFill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (resizeMode) {
|
|
|
|
case UIViewContentModeScaleToFill: // stretch
|
|
|
|
|
|
|
|
sourceSize.width = MIN(destSize.width, sourceSize.width);
|
|
|
|
sourceSize.height = MIN(destSize.height, sourceSize.height);
|
|
|
|
return (CGRect){CGPointZero, sourceSize};
|
|
|
|
|
|
|
|
case UIViewContentModeScaleAspectFit: // contain
|
|
|
|
|
|
|
|
if (targetAspect <= aspect) { // target is taller than content
|
|
|
|
sourceSize.width = destSize.width = MIN(sourceSize.width, destSize.width);
|
|
|
|
sourceSize.height = sourceSize.width / aspect;
|
|
|
|
} else { // target is wider than content
|
|
|
|
sourceSize.height = destSize.height = MIN(sourceSize.height, destSize.height);
|
|
|
|
sourceSize.width = sourceSize.height * aspect;
|
|
|
|
}
|
|
|
|
return (CGRect){CGPointZero, sourceSize};
|
|
|
|
|
|
|
|
case UIViewContentModeScaleAspectFill: // cover
|
|
|
|
|
|
|
|
if (targetAspect <= aspect) { // target is taller than content
|
|
|
|
|
|
|
|
sourceSize.height = destSize.height = MIN(sourceSize.height, destSize.height);
|
|
|
|
sourceSize.width = sourceSize.height * aspect;
|
|
|
|
destSize.width = destSize.height * targetAspect;
|
|
|
|
return (CGRect){{(destSize.width - sourceSize.width) / 2, 0}, sourceSize};
|
|
|
|
|
|
|
|
} else { // target is wider than content
|
|
|
|
|
|
|
|
sourceSize.width = destSize.width = MIN(sourceSize.width, destSize.width);
|
|
|
|
sourceSize.height = sourceSize.width / aspect;
|
|
|
|
destSize.height = destSize.width / targetAspect;
|
|
|
|
return (CGRect){{0, (destSize.height - sourceSize.height) / 2}, sourceSize};
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
RCTLogError(@"A resizeMode value of %zd is not supported", resizeMode);
|
|
|
|
return (CGRect){CGPointZero, destSize};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)downloadImageForURL:(NSURL *)url
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
|
|
|
resizeMode:(UIViewContentMode)resizeMode
|
|
|
|
backgroundColor:(UIColor *)backgroundColor
|
|
|
|
block:(RCTImageDownloadBlock)block
|
2015-01-30 01:10:49 +00:00
|
|
|
{
|
2015-03-06 17:54:10 +00:00
|
|
|
return [self downloadDataForURL:url block:^(NSData *data, NSError *error) {
|
2015-02-19 01:48:13 +00:00
|
|
|
|
2015-05-22 14:17:08 +00:00
|
|
|
if (!data || error) {
|
|
|
|
block(nil, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CGSizeEqualToSize(size, CGSizeZero)) {
|
|
|
|
// Target size wasn't available yet, so abort image drawing
|
|
|
|
block(nil, nil);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:51:14 +00:00
|
|
|
UIImage *image = [UIImage imageWithData:data scale:scale];
|
|
|
|
if (image) {
|
2015-03-06 17:54:10 +00:00
|
|
|
|
2015-05-22 14:17:08 +00:00
|
|
|
// Get scale and size
|
|
|
|
CGFloat destScale = scale ?: RCTScreenScale();
|
|
|
|
CGRect imageRect = RCTClipRect(image.size, image.scale, size, destScale, resizeMode);
|
|
|
|
CGSize destSize = RCTTargetSizeForClipRect(imageRect);
|
2015-02-19 01:51:14 +00:00
|
|
|
|
2015-05-22 14:17:08 +00:00
|
|
|
// Opacity optimizations
|
|
|
|
UIColor *blendColor = nil;
|
|
|
|
BOOL opaque = !RCTImageHasAlpha(image.CGImage);
|
|
|
|
if (!opaque && backgroundColor) {
|
|
|
|
CGFloat alpha;
|
|
|
|
[backgroundColor getRed:NULL green:NULL blue:NULL alpha:&alpha];
|
|
|
|
if (alpha > 0.999) { // no benefit to blending if background is translucent
|
|
|
|
opaque = YES;
|
|
|
|
blendColor = backgroundColor;
|
|
|
|
}
|
2015-02-19 01:51:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 17:54:10 +00:00
|
|
|
// Decompress image at required size
|
2015-05-22 14:17:08 +00:00
|
|
|
UIGraphicsBeginImageContextWithOptions(destSize, opaque, destScale);
|
|
|
|
if (blendColor) {
|
|
|
|
[blendColor setFill];
|
|
|
|
UIRectFill((CGRect){CGPointZero, destSize});
|
|
|
|
}
|
|
|
|
[image drawInRect:imageRect];
|
2015-02-19 01:51:14 +00:00
|
|
|
image = UIGraphicsGetImageFromCurrentImageContext();
|
|
|
|
UIGraphicsEndImageContext();
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
2015-03-31 03:12:32 +00:00
|
|
|
block(image, nil);
|
2015-01-30 01:10:49 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cancelDownload:(id)downloadToken
|
|
|
|
{
|
2015-02-19 01:48:13 +00:00
|
|
|
if (downloadToken) {
|
2015-03-31 03:12:32 +00:00
|
|
|
((dispatch_block_t)downloadToken)();
|
2015-02-19 01:48:13 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|