Skip to content

A propper way to load images #335

@IvanIhnatsiuk

Description

@IvanIhnatsiuk

Problem

Currently, if we have multiple images with the same URL, we will fire multiple requests for them. It's okay when we have different small images. But if someone, for some reason, inserts a lot of full hd images with the same url it will take longer to download all of them separately.

Critical problems:

  1. Unnecessary CPU pressure
  2. Additional work for servers
  3. Additional network data usage.

Solution

We should implement/extend react-native's Imageloader or add our own image loader with SDWebImage for ios or GlideLoader for Android.

Here is a simple example with SDWebImageLoader that we currently use in our app:

#import "EnrichedImageLoader.h"
#import <React/RCTUtils.h>

@implementation EnrichedImageLoader
+ (instancetype)shared {
  static EnrichedImageLoader *shared;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    shared = [self new];
    shared.sdManager = [SDWebImageManager sharedManager];
  });
  return shared;
}

- (void)loadImage:(NSURL *)url completion:(void (^)(UIImage *))completion {
  if (!url) {
    completion(nil);
    return;
  }

  if (RCTIsLocalAssetURL(url)) {
    completion(RCTImageFromLocalAssetURL(url));
    return;
  }

  [self.sdManager
      loadImageWithURL:url
               options:SDWebImageHighPriority
              progress:nil
             completed:^(UIImage *_Nullable image, NSData *_Nullable data,
                         NSError *_Nullable error, SDImageCacheType cacheType,
                         BOOL finished, NSURL *_Nullable imageURL) {
               completion(image);
             }];
}

- (void)loadImage:(NSURL *)url
          headers:(NSDictionary<NSString *, NSString *> *)headers
       completion:(void (^)(UIImage *image))completion {
  if (!url) {
    completion(nil);
    return;
  }

  SDWebImageDownloaderRequestModifier *requestModifier =
      [SDWebImageDownloaderRequestModifier
          requestModifierWithBlock:^NSURLRequest *_Nullable(
              NSURLRequest *_Nonnull request) {
            NSMutableURLRequest *modifiedRequest = [request mutableCopy];
            for (NSString *key in headers) {
              [modifiedRequest setValue:headers[key] forHTTPHeaderField:key];
            }
            return modifiedRequest;
          }];

  SDWebImageContext *context =
      @{SDWebImageContextDownloadRequestModifier : requestModifier};

  [self.sdManager
      loadImageWithURL:url
               options:SDWebImageHighPriority
               context:context
              progress:nil
             completed:^(UIImage *_Nullable image, NSData *_Nullable data,
                         NSError *_Nullable error, SDImageCacheType cacheType,
                         BOOL finished, NSURL *_Nullable imageURL) {
               completion(image);
             }];
}

@end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions