PHP 7.4.33
Preview: RCTAnimatedImage.mm Size: 4.76 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/react-native/Libraries/Image/RCTAnimatedImage.mm
/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#import <ImageIO/ImageIO.h>
#import <React/RCTAnimatedImage.h>

@interface RCTGIFCoderFrame : NSObject

@property (nonatomic, assign) NSUInteger index;
@property (nonatomic, assign) NSTimeInterval duration;

@end

@implementation RCTGIFCoderFrame
@end

@implementation RCTAnimatedImage {
  CGImageSourceRef _imageSource;
  CGFloat _scale;
  NSUInteger _loopCount;
  NSUInteger _frameCount;
  NSArray<RCTGIFCoderFrame *> *_frames;
}

- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale
{
  if (self = [super init]) {
    CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    if (!imageSource) {
      return nil;
    }

    BOOL framesValid = [self scanAndCheckFramesValidWithSource:imageSource];
    if (!framesValid) {
      CFRelease(imageSource);
      return nil;
    }

    _imageSource = imageSource;

    // grab image at the first index
    UIImage *image = [self animatedImageFrameAtIndex:0];
    if (!image) {
      return nil;
    }
    self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:image.imageOrientation];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveMemoryWarning:)
                                                 name:UIApplicationDidReceiveMemoryWarningNotification
                                               object:nil];
  }

  return self;
}

- (BOOL)scanAndCheckFramesValidWithSource:(CGImageSourceRef)imageSource
{
  if (!imageSource) {
    return NO;
  }
  NSUInteger frameCount = CGImageSourceGetCount(imageSource);
  NSUInteger loopCount = [self imageLoopCountWithSource:imageSource];
  NSMutableArray<RCTGIFCoderFrame *> *frames = [NSMutableArray array];

  for (size_t i = 0; i < frameCount; i++) {
    RCTGIFCoderFrame *frame = [RCTGIFCoderFrame new];
    frame.index = i;
    frame.duration = [self frameDurationAtIndex:i source:imageSource];
    [frames addObject:frame];
  }

  _frameCount = frameCount;
  _loopCount = loopCount;
  _frames = [frames copy];

  return YES;
}

- (NSUInteger)imageLoopCountWithSource:(CGImageSourceRef)source
{
  NSUInteger loopCount = 1;
  NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil);
  NSDictionary *gifProperties = imageProperties[(__bridge NSString *)kCGImagePropertyGIFDictionary];
  if (gifProperties) {
    NSNumber *gifLoopCount = gifProperties[(__bridge NSString *)kCGImagePropertyGIFLoopCount];
    if (gifLoopCount != nil) {
      loopCount = gifLoopCount.unsignedIntegerValue;
      if (@available(iOS 14.0, *)) {
      } else {
        // A loop count of 1 means it should animate twice, 2 means, thrice, etc.
        if (loopCount != 0) {
          loopCount++;
        }
      }
    }
  }
  return loopCount;
}

- (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source
{
  float frameDuration = 0.1f;
  CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
  if (!cfFrameProperties) {
    return frameDuration;
  }
  NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
  NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];

  NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
  if (delayTimeUnclampedProp != nil && [delayTimeUnclampedProp floatValue] != 0.0f) {
    frameDuration = [delayTimeUnclampedProp floatValue];
  } else {
    NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
    if (delayTimeProp != nil) {
      frameDuration = [delayTimeProp floatValue];
    }
  }

  CFRelease(cfFrameProperties);
  return frameDuration;
}

- (NSUInteger)animatedImageLoopCount
{
  return _loopCount;
}

- (NSUInteger)animatedImageFrameCount
{
  return _frameCount;
}

- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index
{
  if (index >= _frameCount) {
    return 0;
  }
  return _frames[index].duration;
}

- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index
{
  CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL);
  if (!imageRef) {
    return nil;
  }
  UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:_scale orientation:UIImageOrientationUp];
  CGImageRelease(imageRef);
  return image;
}

- (void)didReceiveMemoryWarning:(NSNotification *)notification
{
  if (_imageSource) {
    for (size_t i = 0; i < _frameCount; i++) {
      CGImageSourceRemoveCacheAtIndex(_imageSource, i);
    }
  }
}

- (void)dealloc
{
  if (_imageSource) {
    CFRelease(_imageSource);
    _imageSource = NULL;
  }
}

@end

Directory Contents

Dirs: 0 × Files: 70
Name Size Perms Modified Actions
305 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
6.37 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
1.24 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
10.57 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
10.79 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
7.92 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
306 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
465 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
3.14 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
2.52 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
7.11 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
1.47 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.12 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
2.81 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
3.94 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
2.36 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
2.20 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
595 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
4.93 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
406 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
440 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
424 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
1.69 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
430 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
414 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
609 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.76 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
302 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.16 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
569 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
841 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
299 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.51 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
364 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.58 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.20 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
5.74 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.85 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
295 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.38 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.42 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
51.56 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
816 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
6.15 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.01 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.10 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.04 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
276 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
380 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.69 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
8.35 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
3.13 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.84 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
707 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
3.50 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
12.22 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
844 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
15.24 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
279 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.53 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
362 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.16 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.53 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
623 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
310 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
10.67 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
2.43 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
710 B lrw-r--r-- 2025-03-28 11:04:42
Edit Download
4.22 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
1.30 KB lrw-r--r-- 2025-03-28 11:04:42
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).