react-native/ReactKit/Base/RCTImageDownloader.m
Christopher Chedeau 6153fffb30 2015-02-03 updates
- Add back providesModule transform to JSAppServer | Joseph Savona
- [ReactKit] fix open source performance issue | John Harper
- [ReactKit] improve ReactIOSEventEmitter logics | Andrew Rasmussen
- [reactkit] fix web view JS executor and bind it to Command-d | John Harper
- Removed hardcoded RCTModuleIDs | Nick Lockwood
- [ReactKit] Animated GIF support | Alex Akers
- [ReactKit] Update RCTBridge to support non-`id` argument types | Alex Akers
- [reactkit] fix typo in RCTCopyProperty() change | John Harper
- [reactkit] fix shadow view crash on missing properties | John Harper
- [reactkit] fix transform keypath | John Harper
2015-02-03 16:15:20 -08:00

82 lines
2.1 KiB
Objective-C

// Copyright 2004-present Facebook. All Rights Reserved.
#import "RCTImageDownloader.h"
#import "RCTUtils.h"
// TODO: something a bit more sophisticated
@implementation RCTImageDownloader
+ (instancetype)sharedInstance
{
RCTAssertMainThread();
static RCTImageDownloader *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (id)downloadDataForURL:(NSURL *)url
block:(RCTDataDownloadBlock)block
{
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Dispatch back to main thread
dispatch_async(dispatch_get_main_queue(), ^{
block(data, error);
});
}];
[task resume];
return task;
}
- (id)downloadImageForURL:(NSURL *)url
size:(CGSize)size
scale:(CGFloat)scale
block:(RCTImageDownloadBlock)block
{
NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
UIImage *image = [UIImage imageWithData:data scale:scale];
// TODO: cache compressed image
CGSize imageSize = size;
if (CGSizeEqualToSize(imageSize, CGSizeZero)) {
imageSize = image.size;
}
CGFloat imageScale = scale;
if (imageScale == 0 || imageScale > image.scale) {
imageScale = image.scale;
}
if (image) {
// Decompress on background thread
UIGraphicsBeginImageContextWithOptions(imageSize, NO, imageScale);
[image drawInRect:(CGRect){{0, 0}, imageSize}];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// TODO: cache decompressed images at each requested size
}
// Dispatch back to main thread
dispatch_async(dispatch_get_main_queue(), ^{
block(image, error);
});
}];
[task resume];
return task;
}
- (void)cancelDownload:(id)downloadToken
{
[(NSURLSessionDataTask *)downloadToken cancel];
}
@end