mirror of
https://github.com/status-im/react-native.git
synced 2025-01-09 17:15:54 +00:00
f88bc3eb73
Summary: @public This is a refactor of @philikon's original diff that decouples the dependencies between the Network and Image modules, and replaces RCTDataQueryExecutor with a more useful abstraction. I've introduced the RCTURLRequestHandler protocol, which is a new type of bridge module used for loading data using an NSURLRequest. RCTURLRequestHandlers can be registered using RCT_EXPORT_MODULE() and are then available at runtime for use by the RCTDataManager, which will automatically select the appropriate handler for a given request based on the handler's self-reported capabilities. The currently implemented handlers are: - RCTHTTPRequestHandler - the standard open source HTTP request handler that uses NSURLSession - RKHTTPRequestHandler - the internal FB HTTP request handler that uses FBNetworking - RCTImageRequestHandler - a handler for loading local images from the iOS asset-library Depends on D2108193 Test Plan: - Internal apps still work - OSS port still compiles, Movies app and a sample Parse app still work - uploading image to Parse using the above code snippet works - tested `FormData` with string and image parameters using http://www.posttestserver.com/
63 lines
1.7 KiB
Objective-C
63 lines
1.7 KiB
Objective-C
//
|
|
// RCTImageRequestHandler.m
|
|
// RCTImage
|
|
//
|
|
// Created by Nick Lockwood on 09/06/2015.
|
|
// Copyright (c) 2015 Facebook. All rights reserved.
|
|
//
|
|
|
|
#import "RCTImageRequestHandler.h"
|
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
#import "RCTImageLoader.h"
|
|
#import "RCTUtils.h"
|
|
|
|
@implementation RCTImageRequestHandler
|
|
{
|
|
NSInteger _currentToken;
|
|
}
|
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
- (BOOL)canHandleRequest:(NSURLRequest *)request
|
|
{
|
|
return [@[@"assets-library", @"ph"] containsObject:[request.URL.scheme lowercaseString]];
|
|
}
|
|
|
|
- (id)sendRequest:(NSURLRequest *)request
|
|
withDelegate:(id<RCTURLRequestDelegate>)delegate
|
|
{
|
|
NSNumber *requestToken = @(++_currentToken);
|
|
NSString *URLString = [request.URL absoluteString];
|
|
[RCTImageLoader loadImageWithTag:URLString 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;
|
|
}
|
|
|
|
@end
|