2015-12-08 11:29:08 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-12-08 11:29:08 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-12-08 11:29:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTImageSource.h"
|
2015-12-21 18:03:59 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-12-08 11:29:08 +00:00
|
|
|
|
|
|
|
@interface RCTImageSource ()
|
|
|
|
|
|
|
|
@property (nonatomic, assign) BOOL packagerAsset;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTImageSource
|
|
|
|
|
2016-06-01 17:32:20 +00:00
|
|
|
- (instancetype)initWithURLRequest:(NSURLRequest *)request size:(CGSize)size scale:(CGFloat)scale
|
2015-12-08 11:29:08 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2016-06-01 17:32:20 +00:00
|
|
|
_request = [request copy];
|
2015-12-08 11:29:08 +00:00
|
|
|
_size = size;
|
|
|
|
_scale = scale;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)imageSourceWithSize:(CGSize)size scale:(CGFloat)scale
|
|
|
|
{
|
2016-06-01 17:32:20 +00:00
|
|
|
RCTImageSource *imageSource = [[RCTImageSource alloc] initWithURLRequest:_request
|
|
|
|
size:size
|
|
|
|
scale:scale];
|
2015-12-08 11:29:08 +00:00
|
|
|
imageSource.packagerAsset = _packagerAsset;
|
|
|
|
return imageSource;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isEqual:(RCTImageSource *)object
|
|
|
|
{
|
|
|
|
if (![object isKindOfClass:[RCTImageSource class]]) {
|
|
|
|
return NO;
|
|
|
|
}
|
2016-06-01 17:32:20 +00:00
|
|
|
return [_request isEqual:object.request] && _scale == object.scale &&
|
2015-12-08 11:29:08 +00:00
|
|
|
(CGSizeEqualToSize(_size, object.size) || CGSizeEqualToSize(object.size, CGSizeZero));
|
|
|
|
}
|
|
|
|
|
2016-09-01 00:30:50 +00:00
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"<RCTImageSource: %p URL=%@, size=%@, scale=%0.f>",
|
|
|
|
self, _request.URL, NSStringFromCGSize(_size), _scale];
|
|
|
|
}
|
|
|
|
|
2015-12-08 11:29:08 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTConvert (ImageSource)
|
|
|
|
|
|
|
|
+ (RCTImageSource *)RCTImageSource:(id)json
|
|
|
|
{
|
|
|
|
if (!json) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:20 +00:00
|
|
|
NSURLRequest *request;
|
2015-12-08 11:29:08 +00:00
|
|
|
CGSize size = CGSizeZero;
|
|
|
|
CGFloat scale = 1.0;
|
|
|
|
BOOL packagerAsset = NO;
|
|
|
|
if ([json isKindOfClass:[NSDictionary class]]) {
|
2016-06-01 17:32:20 +00:00
|
|
|
if (!(request = [self NSURLRequest:json])) {
|
2015-12-08 11:29:08 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
size = [self CGSize:json];
|
2017-06-23 20:03:07 +00:00
|
|
|
scale = [self CGFloat:json[@"scale"]] ?: [self BOOL:json[@"deprecated"]] ? 0.0 : 1.0;
|
2015-12-08 11:29:08 +00:00
|
|
|
packagerAsset = [self BOOL:json[@"__packager_asset"]];
|
|
|
|
} else if ([json isKindOfClass:[NSString class]]) {
|
2016-06-01 17:32:20 +00:00
|
|
|
request = [self NSURLRequest:json];
|
2015-12-08 11:29:08 +00:00
|
|
|
} else {
|
|
|
|
RCTLogConvertError(json, @"an image. Did you forget to call resolveAssetSource() on the JS side?");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2016-06-01 17:32:20 +00:00
|
|
|
RCTImageSource *imageSource = [[RCTImageSource alloc] initWithURLRequest:request
|
2016-09-01 00:30:50 +00:00
|
|
|
size:size
|
|
|
|
scale:scale];
|
2015-12-08 11:29:08 +00:00
|
|
|
imageSource.packagerAsset = packagerAsset;
|
|
|
|
return imageSource;
|
|
|
|
}
|
|
|
|
|
2016-07-28 20:58:50 +00:00
|
|
|
RCT_ARRAY_CONVERTER(RCTImageSource)
|
|
|
|
|
2015-12-08 11:29:08 +00:00
|
|
|
@end
|