mirror of
https://github.com/status-im/react-native.git
synced 2025-01-10 17:45:59 +00:00
2b657003b7
Summary: Hi, I'm currently building an app that changes metadata, does some resizes, maybe watermarking ...etc. I want to use RCTImageStoreManager to store the original image in memory and allow me to command different modifications from javascript as it gives me more flexibility. As RCTImageEditingManager does for example. But currently the RTCImageStoreManager uses UIImage to store the image, the problem is that UIImage losses metadata. So i suggest we change it to NSData. Additionally I added a method to remove an image from the store. A related PR can be found here https://github.com/lwansbrough/react-native-camera/pull/100. Closes https://github.com/facebook/react-native/pull/3290 Reviewed By: javache Differential Revision: D2647271 Pulled By: nicklockwood fb-gh-sync-id: e66353ae3005423beee72ec22189dcb117fc719f
139 lines
3.3 KiB
Objective-C
139 lines
3.3 KiB
Objective-C
/**
|
|
* 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 "RCTNetworkTask.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
@implementation RCTNetworkTask
|
|
{
|
|
NSMutableData *_data;
|
|
id<RCTURLRequestHandler> _handler;
|
|
RCTNetworkTask *_selfReference;
|
|
}
|
|
|
|
- (instancetype)initWithRequest:(NSURLRequest *)request
|
|
handler:(id<RCTURLRequestHandler>)handler
|
|
completionBlock:(RCTURLRequestCompletionBlock)completionBlock
|
|
{
|
|
RCTAssertParam(request);
|
|
RCTAssertParam(handler);
|
|
RCTAssertParam(completionBlock);
|
|
|
|
static NSUInteger requestID = 0;
|
|
|
|
if ((self = [super init])) {
|
|
_requestID = @(requestID++);
|
|
_request = request;
|
|
_handler = handler;
|
|
_completionBlock = completionBlock;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)init)
|
|
|
|
- (void)invalidate
|
|
{
|
|
_selfReference = nil;
|
|
_completionBlock = nil;
|
|
_downloadProgressBlock = nil;
|
|
_incrementalDataBlock = nil;
|
|
_responseBlock = nil;
|
|
_uploadProgressBlock = nil;
|
|
}
|
|
|
|
- (void)start
|
|
{
|
|
if (_requestToken == nil) {
|
|
if ([self validateRequestToken:[_handler sendRequest:_request
|
|
withDelegate:self]]) {
|
|
_selfReference = self;
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)cancel
|
|
{
|
|
__strong id strongToken = _requestToken;
|
|
if (strongToken && [_handler respondsToSelector:@selector(cancelRequest:)]) {
|
|
[_handler cancelRequest:strongToken];
|
|
}
|
|
[self invalidate];
|
|
}
|
|
|
|
- (BOOL)validateRequestToken:(id)requestToken
|
|
{
|
|
if (_requestToken == nil) {
|
|
if (requestToken == nil) {
|
|
return NO;
|
|
}
|
|
_requestToken = requestToken;
|
|
}
|
|
if (![requestToken isEqual:_requestToken]) {
|
|
if (RCT_DEBUG) {
|
|
RCTLogError(@"Unrecognized request token: %@ expected: %@", requestToken, _requestToken);
|
|
}
|
|
if (_completionBlock) {
|
|
_completionBlock(_response, _data, [NSError errorWithDomain:RCTErrorDomain code:0
|
|
userInfo:@{NSLocalizedDescriptionKey: @"Unrecognized request token."}]);
|
|
[self invalidate];
|
|
}
|
|
return NO;
|
|
}
|
|
return YES;
|
|
}
|
|
|
|
- (void)URLRequest:(id)requestToken didSendDataWithProgress:(int64_t)bytesSent
|
|
{
|
|
if ([self validateRequestToken:requestToken]) {
|
|
if (_uploadProgressBlock) {
|
|
_uploadProgressBlock(bytesSent, _request.HTTPBody.length);
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveResponse:(NSURLResponse *)response
|
|
{
|
|
if ([self validateRequestToken:requestToken]) {
|
|
_response = response;
|
|
if (_responseBlock) {
|
|
_responseBlock(response);
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)URLRequest:(id)requestToken didReceiveData:(NSData *)data
|
|
{
|
|
if ([self validateRequestToken:requestToken]) {
|
|
if (!_data) {
|
|
_data = [NSMutableData new];
|
|
}
|
|
[_data appendData:data];
|
|
if (_incrementalDataBlock) {
|
|
_incrementalDataBlock(data);
|
|
}
|
|
if (_downloadProgressBlock && _response.expectedContentLength > 0) {
|
|
_downloadProgressBlock(_data.length, _response.expectedContentLength);
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)URLRequest:(id)requestToken didCompleteWithError:(NSError *)error
|
|
{
|
|
if ([self validateRequestToken:requestToken]) {
|
|
if (_completionBlock) {
|
|
_completionBlock(_response, _data, error);
|
|
[self invalidate];
|
|
}
|
|
}
|
|
}
|
|
|
|
@end
|