Commit 6fc8611f authored by Gaëtan Renaudeau's avatar Gaëtan Renaudeau

iOS: use RCTImageSource to support exactly the same Image source

parent bcb07dcb
......@@ -11,18 +11,10 @@
#import "GLImage.h"
#import "GLRenderData.h"
#import "UIView+React.h"
#import "RCTImageSource.h"
NSString* srcResource (id res)
{
NSString *src;
if ([res isKindOfClass:[NSString class]]) {
src = [RCTConvert NSString:res];
} else {
BOOL isStatic = [RCTConvert BOOL:res[@"isStatic"]];
src = [RCTConvert NSString:res[@"path"]];
if (!src || isStatic) src = [RCTConvert NSString:res[@"uri"]];
}
return src;
NSString* imageSourceHash (RCTImageSource *is) {
return is.imageURL.absoluteString;
}
NSArray* diff (NSArray* a, NSArray* b) {
......@@ -250,27 +242,31 @@ RCT_NOT_IMPLEMENTED(-init)
textures[uniformName] = fbo.color[0];
}
else if ([type isEqualToString:@"uri"]) {
NSString *src = srcResource(value);
RCTImageSource *src = [RCTConvert RCTImageSource:value];
if (!src) {
RCTLogError(@"texture uniform '%@': Invalid uri format '%@'", uniformName, value);
GLTexture *emptyTexture = [[GLTexture alloc] init];
[emptyTexture setPixels:nil];
textures[uniformName] = emptyTexture;
}
GLImage *image = images[src];
else {
NSString *key = imageSourceHash(src);
GLImage *image = images[key];
if (image == nil) {
image = prevImages[src];
image = prevImages[key];
if (image != nil)
images[src] = image;
images[key] = image;
}
if (image == nil) {
__weak GLCanvas *weakSelf = self;
image = [[GLImage alloc] initWithBridge:_bridge withOnLoad:^{
if (weakSelf) [weakSelf onImageLoad:src];
}];
image.src = src;
images[src] = image;
image.source = src;
images[key] = image;
}
textures[uniformName] = [image getTexture];
}
}
else {
RCTLogError(@"texture uniform '%@': Unexpected type '%@'", uniformName, type);
}
......@@ -354,7 +350,7 @@ RCT_NOT_IMPLEMENTED(-init)
- (BOOL)haveRemainingToPreload
{
for (id res in _imagesToPreload) {
if (![_preloaded containsObject:srcResource(res)]) {
if (![_preloaded containsObject:imageSourceHash([RCTConvert RCTImageSource:res])]) {
return true;
}
}
......@@ -552,9 +548,9 @@ RCT_NOT_IMPLEMENTED(-init)
//// utility methods
- (void)onImageLoad:(NSString *)loaded
- (void)onImageLoad:(RCTImageSource *)source
{
[_preloaded addObject:loaded];
[_preloaded addObject:imageSourceHash(source)];
int count = [self countPreloaded];
int total = (int) [_imagesToPreload count];
double progress = ((double) count) / ((double) total);
......@@ -567,7 +563,7 @@ RCT_NOT_IMPLEMENTED(-init)
{
int nb = 0;
for (id toload in _imagesToPreload) {
if ([_preloaded containsObject:srcResource(toload)])
if ([_preloaded containsObject:imageSourceHash([RCTConvert RCTImageSource:toload])])
nb++;
}
return nb;
......
#import "RCTBridge.h"
#import "GLTexture.h"
#import "RCTImageSource.h"
@interface GLImage: NSObject
@property (nonatomic, copy) NSString *src;
@property (nonatomic, copy) RCTImageSource *source;
@property (nonatomic) UIImage *image;
......
......@@ -60,10 +60,10 @@ RCT_NOT_IMPLEMENTED(-init)
return _texture;
}
- (void)setSrc:(NSString *)src
- (void)setSource:(RCTImageSource *)source
{
if (![src isEqualToString:_src]) {
_src = [src copy];
if (![source isEqual:_source]) {
_source = source;
[self reloadImage];
}
}
......@@ -72,22 +72,15 @@ RCT_NOT_IMPLEMENTED(-init)
{
if (_loading) _loading();
_loading = nil;
if (!_src) {
if (!_source) {
[self clearImage];
} else {
}
else {
// Load the image (without resizing it)
if (![_src hasPrefix:@"http://"] && ![_src hasPrefix:@"https://"]) {
self.image = [UIImage imageNamed:_src];
dispatch_async(dispatch_get_main_queue(), ^{
if (_onload) _onload();
});
} else {
_loading = [_bridge.imageLoader loadImageWithTag:_src
_loading = [_bridge.imageLoader loadImageWithoutClipping:_source.imageURL.absoluteString
size:CGSizeZero
scale:0
resizeMode:UIViewContentModeScaleToFill
resizeMode:RCTResizeModeStretch
progressBlock:nil
completionBlock:^(NSError *error, UIImage *image) {
_loading = nil;
......@@ -96,6 +89,7 @@ RCT_NOT_IMPLEMENTED(-init)
NSLog(@"Image failed to load: %@", error);
} else {
// we need to copy the image because it seems the image will be altered.
// ^^^ FIXME: check if it's still the case
self.image = [UIImage imageWithCGImage:image.CGImage];
dispatch_async(dispatch_get_main_queue(), ^{
if (_onload) _onload();
......@@ -103,7 +97,6 @@ RCT_NOT_IMPLEMENTED(-init)
}
}];
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment