GLImage.m 2.76 KB
Newer Older
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
1

2 3
#import "GLImage.h"
#import "GLImageData.h"
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
4 5 6
#import "RCTBridge.h"
#import "RCTImageLoader.h"
#import "RCTLog.h"
7
#import "GLTexture.h"
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
8

9
@implementation GLImage
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
10 11 12
{
  RCTBridge *_bridge; // React's bridge allow to access the imageLoader
  UIImage *_image; // The currently loaded image (nil if no image fully loaded yet)
13 14
  GLImageData *_data; // Cache of the data related to this image (computed by getImageData)
  GLTexture *_texture; // Cache of the texture
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
15 16 17 18 19 20 21 22 23 24 25
  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;
26
    _texture = [[GLTexture alloc] init];
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
27 28 29 30 31 32 33 34 35 36 37
  }
  return self;
}

- (void)dealloc
{
  [self clearImage];
  if (_loading) _loading();
  _onload = nil;
  _loading = nil;
  _bridge = nil;
38
  _texture = nil;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
39 40 41 42 43 44 45 46 47 48
}

RCT_NOT_IMPLEMENTED(-init)

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

49
- (GLTexture *) getTexture
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
50
{
51 52
  if (_image) {
    if (!_data) {
53
        _data = [GLImageData genPixelsWithImage:_image];
54 55 56 57
    }
    [_texture setPixels:_data];
  }
  else {
58
      [_texture setPixels:nil];
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
59
  }
60
  return _texture;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
61 62
}

63
- (void)setSource:(RCTImageSource *)source
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
64
{
65 66
  if (![source isEqual:_source]) {
    _source = source;
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
67 68 69 70 71 72 73 74
    [self reloadImage];
  }
}

- (void)reloadImage
{
  if (_loading) _loading();
  _loading = nil;
75
  if (!_source) {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
76
    [self clearImage];
77 78
  }
  else {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
79
    // Load the image (without resizing it)
Steven Scaffidi's avatar
Steven Scaffidi committed
80
    _loading = [_bridge.imageLoader loadImageWithURLRequest:_source.imageURL.absoluteString
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
81 82
                                       size:CGSizeZero
                                      scale:0
Steven Scaffidi's avatar
Steven Scaffidi committed
83
                                      clipped:YES
84
                                 resizeMode:RCTResizeModeStretch
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
85
                              progressBlock:nil
86
                            completionBlock:^(NSError *error, UIImage *image) {
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
87 88 89 90 91
                              _loading = nil;
                              [self clearImage];
                              if (error) {
                                NSLog(@"Image failed to load: %@", error);
                              } else {
92
                                // we need to copy the image because it seems the image will be altered.
93
                                // ^^^ FIXME: check if it's still the case
94
                                self.image = [UIImage imageWithCGImage:image.CGImage];
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
95 96 97
                                dispatch_async(dispatch_get_main_queue(), ^{
                                  if (_onload) _onload();
                                });
Gaëtan Renaudeau's avatar
Gaëtan Renaudeau committed
98 99 100 101 102 103 104
                              }
                            }];
  }
}


@end