diff --git a/ios/GLCanvas.m b/ios/GLCanvas.m index ad769f91449b8267e872fce0680f1e0a0524ac48..e6eb4b253d02952b2a866740fd37b9ecbb1b874c 100644 --- a/ios/GLCanvas.m +++ b/ios/GLCanvas.m @@ -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,26 +242,30 @@ 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); - } - - GLImage *image = images[src]; - if (image == nil) { - image = prevImages[src]; - if (image != nil) - images[src] = image; + GLTexture *emptyTexture = [[GLTexture alloc] init]; + [emptyTexture setPixels:nil]; + textures[uniformName] = emptyTexture; } - if (image == nil) { - __weak GLCanvas *weakSelf = self; - image = [[GLImage alloc] initWithBridge:_bridge withOnLoad:^{ - if (weakSelf) [weakSelf onImageLoad:src]; - }]; - image.src = src; - images[src] = image; + else { + NSString *key = imageSourceHash(src); + GLImage *image = images[key]; + if (image == nil) { + image = prevImages[key]; + if (image != nil) + images[key] = image; + } + if (image == nil) { + __weak GLCanvas *weakSelf = self; + image = [[GLImage alloc] initWithBridge:_bridge withOnLoad:^{ + if (weakSelf) [weakSelf onImageLoad:src]; + }]; + image.source = src; + images[key] = image; + } + textures[uniformName] = [image getTexture]; } - 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; diff --git a/ios/GLImage.h b/ios/GLImage.h index b30c333533dd3ac88ef09f290b7eb6b4027c6f8c..610cd4c0fa3d17dca80d739253ed9876baa25b8a 100644 --- a/ios/GLImage.h +++ b/ios/GLImage.h @@ -1,10 +1,11 @@ #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; diff --git a/ios/GLImage.m b/ios/GLImage.m index d4c1d3f0d3dbe65bbe8a3dd18c867b254ad71640..f43f26ab8efef5d200b508b98a236b5d495d1063 100644 --- a/ios/GLImage.m +++ b/ios/GLImage.m @@ -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,13 +89,13 @@ 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(); }); } }]; - } } }