2015-07-21 05:44: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTImageStoreManager.h"
|
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
|
|
|
@implementation RCTImageStoreManager
|
|
|
|
{
|
|
|
|
NSMutableDictionary *_store;
|
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize methodQueue = _methodQueue;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
- (instancetype)init
|
2015-07-21 05:44:42 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
|
|
|
|
// TODO: need a way to clear this store
|
2015-08-17 14:35:34 +00:00
|
|
|
_store = [NSMutableDictionary new];
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)storeImage:(UIImage *)image
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
2015-08-24 10:14:33 +00:00
|
|
|
NSString *tag = [NSString stringWithFormat:@"rct-image-store://%tu", _store.count];
|
2015-07-21 05:44:42 +00:00
|
|
|
_store[tag] = image;
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIImage *)imageForTag:(NSString *)imageTag
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
|
|
|
return _store[imageTag];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)storeImage:(UIImage *)image withBlock:(void (^)(NSString *imageTag))block
|
|
|
|
{
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
NSString *imageTag = [self storeImage:image];
|
|
|
|
if (block) {
|
|
|
|
block(imageTag);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)getImageForTag:(NSString *)imageTag withBlock:(void (^)(UIImage *image))block
|
|
|
|
{
|
|
|
|
RCTAssert(block != nil, @"block must not be nil");
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
block([self imageForTag:imageTag]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO (#5906496): Name could be more explicit - something like getBase64EncodedJPEGDataForTag:?
|
|
|
|
RCT_EXPORT_METHOD(getBase64ForTag:(NSString *)imageTag
|
|
|
|
successCallback:(RCTResponseSenderBlock)successCallback
|
|
|
|
errorCallback:(RCTResponseErrorBlock)errorCallback)
|
|
|
|
{
|
|
|
|
[self getImageForTag:imageTag withBlock:^(UIImage *image) {
|
|
|
|
if (!image) {
|
|
|
|
errorCallback(RCTErrorWithMessage([NSString stringWithFormat:@"Invalid imageTag: %@", imageTag]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dispatch_async(_methodQueue, ^{
|
|
|
|
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
|
|
|
|
NSString *base64 = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
|
|
|
|
successCallback(@[[base64 stringByReplacingOccurrencesOfString:@"\n" withString:@""]]);
|
|
|
|
});
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(addImageFromBase64:(NSString *)base64String
|
|
|
|
successCallback:(RCTResponseSenderBlock)successCallback
|
|
|
|
errorCallback:(RCTResponseErrorBlock)errorCallback)
|
|
|
|
|
|
|
|
{
|
|
|
|
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
|
|
|
|
if (imageData) {
|
|
|
|
UIImage *image = [[UIImage alloc] initWithData:imageData];
|
|
|
|
[self storeImage:image withBlock:^(NSString *imageTag) {
|
|
|
|
successCallback(@[imageTag]);
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
errorCallback(RCTErrorWithMessage(@"Failed to add image from base64String"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 15:25:10 +00:00
|
|
|
#pragma mark - RCTImageLoader
|
|
|
|
|
|
|
|
- (BOOL)canLoadImageURL:(NSURL *)requestURL
|
|
|
|
{
|
|
|
|
return [requestURL.scheme.lowercaseString isEqualToString:@"rct-image-store"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL size:(CGSize)size scale:(CGFloat)scale resizeMode:(UIViewContentMode)resizeMode progressHandler:(RCTImageLoaderProgressBlock)progressHandler completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
|
|
|
|
{
|
|
|
|
NSString *imageTag = imageURL.absoluteString;
|
|
|
|
[self getImageForTag:imageTag withBlock:^(UIImage *image) {
|
|
|
|
if (image) {
|
|
|
|
completionHandler(nil, image);
|
|
|
|
} else {
|
|
|
|
NSString *errorMessage = [NSString stringWithFormat:@"Unable to load image from image store: %@", imageTag];
|
|
|
|
NSError *error = RCTErrorWithMessage(errorMessage);
|
|
|
|
completionHandler(error, nil);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2015-07-21 05:44:42 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBridge (RCTImageStoreManager)
|
|
|
|
|
|
|
|
- (RCTImageStoreManager *)imageStoreManager
|
|
|
|
{
|
|
|
|
return self.modules[RCTBridgeModuleNameForClass([RCTImageStoreManager class])];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|