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"
|
|
|
|
|
2017-08-01 09:48:28 +00:00
|
|
|
#import <stdatomic.h>
|
2016-11-23 15:47:52 +00:00
|
|
|
|
|
|
|
#import <ImageIO/ImageIO.h>
|
2015-11-17 17:53:47 +00:00
|
|
|
#import <MobileCoreServices/UTType.h>
|
|
|
|
|
2016-11-23 15:47:52 +00:00
|
|
|
#import <React/RCTAssert.h>
|
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTUtils.h>
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
#import "RCTImageUtils.h"
|
2015-07-21 05:44:42 +00:00
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
static NSString *const RCTImageStoreURLScheme = @"rct-image-store";
|
|
|
|
|
2015-07-21 05:44:42 +00:00
|
|
|
@implementation RCTImageStoreManager
|
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
NSMutableDictionary<NSString *, NSData *> *_store;
|
2016-07-12 20:54:16 +00:00
|
|
|
NSUInteger _id;
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@synthesize methodQueue = _methodQueue;
|
|
|
|
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2017-10-10 05:18:34 +00:00
|
|
|
- (float)handlerPriority
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-30 06:10:39 +00:00
|
|
|
- (void)removeImageForTag:(NSString *)imageTag withBlock:(void (^)(void))block
|
2015-07-21 05:44:42 +00:00
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
dispatch_async(_methodQueue, ^{
|
|
|
|
[self removeImageForTag:imageTag];
|
|
|
|
if (block) {
|
|
|
|
block();
|
|
|
|
}
|
|
|
|
});
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
- (NSString *)_storeImageData:(NSData *)imageData
|
2015-07-21 05:44:42 +00:00
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
RCTAssertThread(_methodQueue, @"Must be called on RCTImageStoreManager thread");
|
2015-11-25 11:09:00 +00:00
|
|
|
|
|
|
|
if (!_store) {
|
|
|
|
_store = [NSMutableDictionary new];
|
|
|
|
_id = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
NSString *imageTag = [NSString stringWithFormat:@"%@://%tu", RCTImageStoreURLScheme, _id++];
|
|
|
|
_store[imageTag] = imageData;
|
|
|
|
return imageTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)storeImageData:(NSData *)imageData withBlock:(void (^)(NSString *imageTag))block
|
|
|
|
{
|
|
|
|
RCTAssertParam(block);
|
|
|
|
dispatch_async(_methodQueue, ^{
|
|
|
|
block([self _storeImageData:imageData]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)getImageDataForTag:(NSString *)imageTag withBlock:(void (^)(NSData *imageData))block
|
|
|
|
{
|
|
|
|
RCTAssertParam(block);
|
|
|
|
dispatch_async(_methodQueue, ^{
|
2016-07-07 19:36:56 +00:00
|
|
|
block(self->_store[imageTag]);
|
2015-11-17 17:53:47 +00:00
|
|
|
});
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)storeImage:(UIImage *)image withBlock:(void (^)(NSString *imageTag))block
|
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
RCTAssertParam(block);
|
|
|
|
dispatch_async(_methodQueue, ^{
|
2017-07-25 04:39:02 +00:00
|
|
|
NSString *imageTag = [self _storeImageData:RCTGetImageData(image, 0.75)];
|
2015-11-17 17:53:47 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2015-07-21 05:44:42 +00:00
|
|
|
block(imageTag);
|
2015-11-17 17:53:47 +00:00
|
|
|
});
|
2015-07-21 05:44:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
RCT_EXPORT_METHOD(removeImageForTag:(NSString *)imageTag)
|
2015-07-21 05:44:42 +00:00
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
[_store removeObjectForKey:imageTag];
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 21:36:37 +00:00
|
|
|
RCT_EXPORT_METHOD(hasImageForTag:(NSString *)imageTag
|
|
|
|
callback:(RCTResponseSenderBlock)callback)
|
|
|
|
{
|
|
|
|
callback(@[@(_store[imageTag] != nil)]);
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
// TODO (#5906496): Name could be more explicit - something like getBase64EncodedDataForTag:?
|
2015-07-21 05:44:42 +00:00
|
|
|
RCT_EXPORT_METHOD(getBase64ForTag:(NSString *)imageTag
|
|
|
|
successCallback:(RCTResponseSenderBlock)successCallback
|
|
|
|
errorCallback:(RCTResponseErrorBlock)errorCallback)
|
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
NSData *imageData = _store[imageTag];
|
|
|
|
if (!imageData) {
|
|
|
|
errorCallback(RCTErrorWithMessage([NSString stringWithFormat:@"Invalid imageTag: %@", imageTag]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Dispatching to a background thread to perform base64 encoding
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
successCallback(@[[imageData base64EncodedStringWithOptions:0]]);
|
|
|
|
});
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(addImageFromBase64:(NSString *)base64String
|
|
|
|
successCallback:(RCTResponseSenderBlock)successCallback
|
|
|
|
errorCallback:(RCTResponseErrorBlock)errorCallback)
|
|
|
|
|
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
// Dispatching to a background thread to perform base64 decoding
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
NSData *imageData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
|
|
|
|
if (imageData) {
|
2016-07-07 19:36:56 +00:00
|
|
|
dispatch_async(self->_methodQueue, ^{
|
2015-11-17 17:53:47 +00:00
|
|
|
successCallback(@[[self _storeImageData:imageData]]);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
errorCallback(RCTErrorWithMessage(@"Failed to add image from base64String"));
|
|
|
|
}
|
|
|
|
});
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
#pragma mark - RCTURLRequestHandler
|
2015-09-02 15:25:10 +00:00
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
- (BOOL)canHandleRequest:(NSURLRequest *)request
|
2015-09-02 15:25:10 +00:00
|
|
|
{
|
2015-11-17 17:53:47 +00:00
|
|
|
return [request.URL.scheme caseInsensitiveCompare:RCTImageStoreURLScheme] == NSOrderedSame;
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
- (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
|
2015-09-02 15:25:10 +00:00
|
|
|
{
|
2017-08-01 09:48:28 +00:00
|
|
|
__block atomic_bool cancelled = ATOMIC_VAR_INIT(NO);
|
2015-11-17 17:53:47 +00:00
|
|
|
void (^cancellationBlock)(void) = ^{
|
2017-08-01 09:48:28 +00:00
|
|
|
atomic_store(&cancelled, YES);
|
2015-11-17 17:53:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Dispatch async to give caller time to cancel the request
|
|
|
|
dispatch_async(_methodQueue, ^{
|
2017-08-01 09:48:28 +00:00
|
|
|
if (atomic_load(&cancelled)) {
|
2015-11-17 17:53:47 +00:00
|
|
|
return;
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 17:53:47 +00:00
|
|
|
NSString *imageTag = request.URL.absoluteString;
|
2016-07-07 19:36:56 +00:00
|
|
|
NSData *imageData = self->_store[imageTag];
|
2015-11-17 17:53:47 +00:00
|
|
|
if (!imageData) {
|
|
|
|
NSError *error = RCTErrorWithMessage([NSString stringWithFormat:@"Invalid imageTag: %@", imageTag]);
|
|
|
|
[delegate URLRequest:cancellationBlock didCompleteWithError:error];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGImageSourceRef sourceRef = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
|
|
|
|
if (!sourceRef) {
|
|
|
|
NSError *error = RCTErrorWithMessage([NSString stringWithFormat:@"Unable to decode data for imageTag: %@", imageTag]);
|
|
|
|
[delegate URLRequest:cancellationBlock didCompleteWithError:error];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CFStringRef UTI = CGImageSourceGetType(sourceRef);
|
|
|
|
CFRelease(sourceRef);
|
|
|
|
|
|
|
|
NSString *MIMEType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
|
|
|
|
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:request.URL
|
|
|
|
MIMEType:MIMEType
|
|
|
|
expectedContentLength:imageData.length
|
|
|
|
textEncodingName:nil];
|
|
|
|
|
|
|
|
[delegate URLRequest:cancellationBlock didReceiveResponse:response];
|
|
|
|
[delegate URLRequest:cancellationBlock didReceiveData:imageData];
|
|
|
|
[delegate URLRequest:cancellationBlock didCompleteWithError:nil];
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return cancellationBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cancelRequest:(id)requestToken
|
|
|
|
{
|
|
|
|
if (requestToken) {
|
|
|
|
((void (^)(void))requestToken)();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTImageStoreManager (Deprecated)
|
|
|
|
|
|
|
|
- (NSString *)storeImage:(UIImage *)image
|
|
|
|
{
|
2016-06-06 14:57:55 +00:00
|
|
|
RCTAssertMainQueue();
|
2015-11-17 17:53:47 +00:00
|
|
|
RCTLogWarn(@"RCTImageStoreManager.storeImage() is deprecated and has poor performance. Use an alternative method instead.");
|
|
|
|
__block NSString *imageTag;
|
|
|
|
dispatch_sync(_methodQueue, ^{
|
2017-07-25 04:39:02 +00:00
|
|
|
imageTag = [self _storeImageData:RCTGetImageData(image, 0.75)];
|
2015-11-17 17:53:47 +00:00
|
|
|
});
|
|
|
|
return imageTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (UIImage *)imageForTag:(NSString *)imageTag
|
|
|
|
{
|
2016-06-06 14:57:55 +00:00
|
|
|
RCTAssertMainQueue();
|
2015-11-17 17:53:47 +00:00
|
|
|
RCTLogWarn(@"RCTImageStoreManager.imageForTag() is deprecated and has poor performance. Use an alternative method instead.");
|
|
|
|
__block NSData *imageData;
|
|
|
|
dispatch_sync(_methodQueue, ^{
|
2016-07-07 19:36:56 +00:00
|
|
|
imageData = self->_store[imageTag];
|
2015-11-17 17:53:47 +00:00
|
|
|
});
|
|
|
|
return [UIImage imageWithData:imageData];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)getImageForTag:(NSString *)imageTag withBlock:(void (^)(UIImage *image))block
|
|
|
|
{
|
|
|
|
RCTAssertParam(block);
|
|
|
|
dispatch_async(_methodQueue, ^{
|
2016-07-07 19:36:56 +00:00
|
|
|
NSData *imageData = self->_store[imageTag];
|
2015-11-17 17:53:47 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2016-01-20 19:03:22 +00:00
|
|
|
// imageWithData: is not thread-safe, so we can't do this on methodQueue
|
2015-11-20 13:15:49 +00:00
|
|
|
block([UIImage imageWithData:imageData]);
|
2015-11-17 17:53:47 +00:00
|
|
|
});
|
|
|
|
});
|
2015-09-02 15:25:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 05:44:42 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTBridge (RCTImageStoreManager)
|
|
|
|
|
|
|
|
- (RCTImageStoreManager *)imageStoreManager
|
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
return [self moduleForClass:[RCTImageStoreManager class]];
|
2015-07-21 05:44:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|