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