2015-03-23 20:28:42 +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-03-10 00:08:01 +00:00
|
|
|
|
|
|
|
#import "RCTImageLoader.h"
|
|
|
|
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
|
|
|
#import "RCTConvert.h"
|
2015-06-09 19:25:33 +00:00
|
|
|
#import "RCTDefines.h"
|
2015-03-10 00:08:01 +00:00
|
|
|
#import "RCTImageDownloader.h"
|
|
|
|
#import "RCTLog.h"
|
2015-06-09 19:25:24 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-03-10 00:08:01 +00:00
|
|
|
|
2015-06-11 17:42:00 +00:00
|
|
|
static void RCTDispatchCallbackOnMainQueue(void (^callback)(NSError *, id), NSError *error, UIImage *image)
|
2015-06-09 19:25:33 +00:00
|
|
|
{
|
|
|
|
if ([NSThread isMainThread]) {
|
|
|
|
callback(error, image);
|
|
|
|
} else {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
callback(error, image);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 11:35:44 +00:00
|
|
|
@implementation UIImage (React)
|
|
|
|
|
|
|
|
- (CAKeyframeAnimation *)reactKeyframeAnimation
|
|
|
|
{
|
|
|
|
return objc_getAssociatedObject(self, _cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setReactKeyframeAnimation:(CAKeyframeAnimation *)reactKeyframeAnimation
|
|
|
|
{
|
|
|
|
objc_setAssociatedObject(self, @selector(reactKeyframeAnimation), reactKeyframeAnimation, OBJC_ASSOCIATION_COPY_NONATOMIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-03-10 00:08:01 +00:00
|
|
|
@implementation RCTImageLoader
|
|
|
|
|
2015-07-27 15:48:31 +00:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageWithTag:(NSString *)imageTag
|
2015-07-15 20:17:13 +00:00
|
|
|
callback:(RCTImageLoaderCompletionBlock)callback
|
2015-07-14 11:06:17 +00:00
|
|
|
{
|
|
|
|
return [self loadImageWithTag:imageTag
|
|
|
|
size:CGSizeZero
|
2015-07-29 22:52:28 +00:00
|
|
|
scale:1
|
2015-07-14 11:06:17 +00:00
|
|
|
resizeMode:UIViewContentModeScaleToFill
|
2015-07-15 20:17:13 +00:00
|
|
|
progressBlock:nil
|
|
|
|
completionBlock:callback];
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
- (id<RCTImageURLLoader>)imageURLLoaderForRequest:(NSURL *)requestURL
|
2015-07-21 12:40:06 +00:00
|
|
|
{
|
2015-09-02 15:25:10 +00:00
|
|
|
NSMutableArray *handlers = [NSMutableArray array];
|
|
|
|
for (id<RCTBridgeModule> module in _bridge.modules.allValues) {
|
|
|
|
if ([module conformsToProtocol:@protocol(RCTImageURLLoader)]) {
|
|
|
|
if ([(id<RCTImageURLLoader>)module canLoadImageURL:requestURL]) {
|
|
|
|
[handlers addObject:module];
|
|
|
|
}
|
|
|
|
}
|
2015-07-21 12:39:57 +00:00
|
|
|
}
|
2015-09-02 15:25:10 +00:00
|
|
|
[handlers sortUsingComparator:^NSComparisonResult(id<RCTImageURLLoader> a, id<RCTImageURLLoader> b) {
|
|
|
|
float priorityA = [a respondsToSelector:@selector(imageLoaderPriority)] ? [a imageLoaderPriority] : 0;
|
|
|
|
float priorityB = [b respondsToSelector:@selector(imageLoaderPriority)] ? [b imageLoaderPriority] : 0;
|
|
|
|
if (priorityA < priorityB) {
|
|
|
|
return NSOrderedAscending;
|
|
|
|
} else if (priorityA > priorityB) {
|
|
|
|
return NSOrderedDescending;
|
|
|
|
} else {
|
|
|
|
RCTLogError(@"The RCTImageLoader %@ and %@ both reported that they can"
|
|
|
|
" handle the load request %@, and have equal priority (%g)."
|
|
|
|
" This could result in non-deterministic behavior.",
|
|
|
|
a, b, requestURL, priorityA);
|
2015-07-21 12:39:57 +00:00
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
return NSOrderedSame;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
return [handlers lastObject];
|
2015-07-27 15:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageWithTag:(NSString *)imageTag
|
2015-07-15 20:17:13 +00:00
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
|
|
|
resizeMode:(UIViewContentMode)resizeMode
|
2015-07-27 20:46:59 +00:00
|
|
|
progressBlock:(RCTImageLoaderProgressBlock)progressBlock
|
|
|
|
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock
|
2015-03-10 00:08:01 +00:00
|
|
|
{
|
2015-09-02 15:25:10 +00:00
|
|
|
NSURL *requestURL = [RCTConvert NSURL:imageTag];
|
|
|
|
id<RCTImageURLLoader> loadHandler = [self imageURLLoaderForRequest:requestURL];
|
|
|
|
if (!loadHandler) {
|
|
|
|
RCTLogError(@"No suitable image URL loader found for %@", imageTag);
|
|
|
|
}
|
2015-07-14 11:06:17 +00:00
|
|
|
|
2015-09-03 12:53:16 +00:00
|
|
|
return [loadHandler loadImageForURL:requestURL size:size scale:scale resizeMode:resizeMode progressHandler:^(int64_t progress, int64_t total) {
|
|
|
|
if (!progressBlock) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([NSThread isMainThread]) {
|
|
|
|
progressBlock(progress, total);
|
|
|
|
} else {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
progressBlock(progress, total);
|
|
|
|
});
|
|
|
|
}
|
2015-09-04 11:35:44 +00:00
|
|
|
} completionHandler:^(NSError *error, UIImage *image) {
|
2015-09-02 15:25:10 +00:00
|
|
|
RCTDispatchCallbackOnMainQueue(completionBlock, error, image);
|
2015-09-03 12:53:16 +00:00
|
|
|
}] ?: ^{};
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
2015-07-14 11:06:17 +00:00
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
- (id<RCTImageDecoder>)imageDecoderForRequest:(NSData *)imageData
|
|
|
|
{
|
|
|
|
NSMutableArray *handlers = [NSMutableArray array];
|
|
|
|
for (id<RCTBridgeModule> module in _bridge.modules.allValues) {
|
|
|
|
if ([module conformsToProtocol:@protocol(RCTImageDecoder)]) {
|
|
|
|
if ([(id<RCTImageDecoder>)module canDecodeImageData:imageData]) {
|
|
|
|
[handlers addObject:module];
|
2015-03-10 00:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
[handlers sortUsingComparator:^NSComparisonResult(id<RCTImageDecoder> a, id<RCTImageDecoder> b) {
|
|
|
|
float priorityA = [a respondsToSelector:@selector(imageDecoderPriority)] ? [a imageDecoderPriority] : 0;
|
|
|
|
float priorityB = [b respondsToSelector:@selector(imageDecoderPriority)] ? [b imageDecoderPriority] : 0;
|
|
|
|
if (priorityA < priorityB) {
|
|
|
|
return NSOrderedAscending;
|
|
|
|
} else if (priorityA > priorityB) {
|
|
|
|
return NSOrderedDescending;
|
2015-07-21 12:39:57 +00:00
|
|
|
} else {
|
2015-09-02 15:25:10 +00:00
|
|
|
RCTLogError(@"The RCTImageDecoder %@ and %@ both reported that they can"
|
|
|
|
" handle the decode request <NSData %p; %tu bytes>, and have"
|
|
|
|
" equal priority (%g). This could result in"
|
|
|
|
" non-deterministic behavior.",
|
|
|
|
a, b, imageData, imageData.length, priorityA);
|
2015-07-21 12:39:57 +00:00
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
return NSOrderedSame;
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
2015-09-02 15:25:10 +00:00
|
|
|
}];
|
|
|
|
return [handlers lastObject];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
|
|
|
|
size:(CGSize)size
|
|
|
|
scale:(CGFloat)scale
|
|
|
|
resizeMode:(UIViewContentMode)resizeMode
|
|
|
|
completionBlock:(RCTImageLoaderCompletionBlock)completionBlock
|
|
|
|
{
|
|
|
|
id<RCTImageDecoder> imageDecoder = [self imageDecoderForRequest:data];
|
|
|
|
if (imageDecoder) {
|
2015-09-04 11:35:44 +00:00
|
|
|
return [imageDecoder decodeImageData:data size:size scale:scale resizeMode:resizeMode completionHandler:^(NSError *error, UIImage *image) {
|
2015-07-27 20:46:59 +00:00
|
|
|
RCTDispatchCallbackOnMainQueue(completionBlock, error, image);
|
|
|
|
}];
|
2015-09-02 15:25:10 +00:00
|
|
|
} else {
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
UIImage *image = [UIImage imageWithData:data];
|
2015-07-21 05:44:42 +00:00
|
|
|
if (image) {
|
2015-07-27 20:46:59 +00:00
|
|
|
RCTDispatchCallbackOnMainQueue(completionBlock, nil, image);
|
2015-07-21 05:44:42 +00:00
|
|
|
} else {
|
2015-09-02 15:25:10 +00:00
|
|
|
NSString *errorMessage = [NSString stringWithFormat:@"Error decoding image data <NSData %p; %tu bytes>", data, data.length];
|
|
|
|
NSError *finalError = RCTErrorWithMessage(errorMessage);
|
|
|
|
RCTDispatchCallbackOnMainQueue(completionBlock, finalError, nil);
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
2015-09-02 15:25:10 +00:00
|
|
|
});
|
2015-07-15 20:17:13 +00:00
|
|
|
return ^{};
|
2015-03-10 00:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
#pragma mark - RCTURLRequestHandler
|
|
|
|
|
|
|
|
- (BOOL)canHandleRequest:(NSURLRequest *)request
|
2015-07-14 11:06:17 +00:00
|
|
|
{
|
2015-09-02 15:25:10 +00:00
|
|
|
id<RCTImageURLLoader> handler = [self imageURLLoaderForRequest:request.URL];
|
|
|
|
|
|
|
|
// RCTImageDownloader is an image plugin that uses the networking stack.
|
|
|
|
// We don't want to route any network calls through the image downloader
|
|
|
|
// as that would cause cyclical dependencies.
|
|
|
|
return handler && ![handler isKindOfClass:[RCTImageDownloader class]];
|
2015-07-15 20:17:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
- (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
|
2015-07-15 20:17:13 +00:00
|
|
|
{
|
2015-09-02 15:25:10 +00:00
|
|
|
__block RCTImageLoaderCancellationBlock requestToken;
|
|
|
|
requestToken = [self.bridge.imageLoader loadImageWithTag:request.URL.absoluteString callback:^(NSError *error, UIImage *image) {
|
|
|
|
if (error) {
|
|
|
|
[delegate URLRequest:requestToken didCompleteWithError:error];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *mimeType = nil;
|
|
|
|
NSData *imageData = nil;
|
|
|
|
if (RCTImageHasAlpha(image.CGImage)) {
|
|
|
|
mimeType = @"image/png";
|
|
|
|
imageData = UIImagePNGRepresentation(image);
|
|
|
|
} else {
|
|
|
|
mimeType = @"image/jpeg";
|
|
|
|
imageData = UIImageJPEGRepresentation(image, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:request.URL
|
|
|
|
MIMEType:mimeType
|
|
|
|
expectedContentLength:imageData.length
|
|
|
|
textEncodingName:nil];
|
|
|
|
|
|
|
|
[delegate URLRequest:requestToken didReceiveResponse:response];
|
|
|
|
[delegate URLRequest:requestToken didReceiveData:imageData];
|
|
|
|
[delegate URLRequest:requestToken didCompleteWithError:nil];
|
|
|
|
}];
|
|
|
|
|
|
|
|
return requestToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cancelRequest:(id)requestToken
|
|
|
|
{
|
|
|
|
if (requestToken) {
|
|
|
|
((RCTImageLoaderCancellationBlock)requestToken)();
|
|
|
|
}
|
2015-07-14 11:06:17 +00:00
|
|
|
}
|
|
|
|
|
2015-03-10 00:08:01 +00:00
|
|
|
@end
|
2015-07-27 15:48:31 +00:00
|
|
|
|
|
|
|
@implementation RCTBridge (RCTImageLoader)
|
|
|
|
|
|
|
|
- (RCTImageLoader *)imageLoader
|
|
|
|
{
|
|
|
|
return self.modules[RCTBridgeModuleNameForClass([RCTImageLoader class])];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|