GLImage.m 3.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

#import <React/RCTBridge.h>
#import <React/RCTImageLoader.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>
#import "GLImage.h"
#import "GLImageData.h"
#import "GLTexture.h"

@implementation GLImage
{
  RCTBridge *_bridge; // React's bridge allow to access the imageLoader
  UIImage *_image; // The currently loaded image (nil if no image fully loaded yet)
  GLImageData *_data; // Cache of the data related to this image (computed by getImageData)
  GLTexture *_texture; // Cache of the texture
  void (^_onload)(void); // called everytime an image loads
  RCTImageLoaderCancellationBlock _loading; // the current loading cancellation function
}

- (instancetype)initWithBridge:(RCTBridge *)bridge withOnLoad:(void (^)(void))onload
{
  if ((self = [super init])) {
    _bridge = bridge;
    _onload = onload;
    _image = nil;
    _loading = nil;
    _texture = [[GLTexture alloc] init];
  }
  return self;
}

- (void)dealloc
{
  [self clearImage];
  if (_loading) _loading();
  _onload = nil;
  _loading = nil;
  _bridge = nil;
  _texture = nil;
}

RCT_NOT_IMPLEMENTED(-init)

- (void) clearImage
{
  _image = nil;
  _data = nil;
}

- (GLTexture *) getTexture
{
  if (_image) {
    if (!_data) {
        _data = [GLImageData genPixelsWithImage:_image];
    }
    [_texture setPixels:_data];
  }
  else {
      [_texture setPixels:nil];
  }
  return _texture;
}

- (void)setSource:(RCTImageSource *)source
{
  if (![source isEqual:_source]) {
    _source = source;
    [self reloadImage];
  }
}

- (void)reloadImage
{
  RCTImageSource *source = _source;
  if (_loading) _loading();
  _loading = nil;
  if (!source) {
    [self clearImage];
  }
  else {
    // Load the image (without resizing it)
    __weak GLImage *weakSelf = self;
    _loading = [_bridge.imageLoader loadImageWithURLRequest:source.request
                                       size:CGSizeZero
                                      scale:0
                                      clipped:YES
                                 resizeMode:RCTResizeModeStretch
                              progressBlock:nil
                              partialLoadBlock:nil
                            completionBlock:^(NSError *error, UIImage *loadedImage) {
                              GLImage *strongSelf = weakSelf;
                              void (^setImageBlock)(UIImage *) = ^(UIImage *image) {
                                if (![source isEqual:strongSelf.source]) {
                                  // Bail out if source has changed since we started loading
                                  return;
                                }
                                strongSelf.image = [UIImage imageWithCGImage:image.CGImage];
                                dispatch_async(dispatch_get_main_queue(), ^{
                                  if (_onload) _onload();
                                });
                              };

                              _loading = nil;
                              [self clearImage];
                              if (error) {
                                NSLog(@"Image failed to load: %@", error);
                              } else {
                                if ([NSThread isMainThread]) {
                                  setImageBlock(loadedImage);
                                } else {
                                  RCTUnsafeExecuteOnMainQueueSync(^{
                                    setImageBlock(loadedImage);
                                  });
                                }
                              }
                            }];
  }
}


@end