From 6fc8611fcb2ce45251fdc329a51d5a793c7f82e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Fri, 19 Feb 2016 17:50:09 +0100 Subject: [PATCH] iOS: use RCTImageSource to support exactly the same Image source --- ios/GLCanvas.m | 60 +++++++++++++++++++++++--------------------------- ios/GLImage.h | 3 ++- ios/GLImage.m | 25 ++++++++------------- 3 files changed, 39 insertions(+), 49 deletions(-) diff --git a/ios/GLCanvas.m b/ios/GLCanvas.m index ad769f9..e6eb4b2 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 b30c333..610cd4c 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 d4c1d3f..f43f26a 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(); }); } }]; - } } } -- 2.26.2