|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import "RCTAnimatedImage.h" |
| 9 | + |
| 10 | +@interface RCTGIFCoderFrame : NSObject |
| 11 | + |
| 12 | +@property (nonatomic, assign) NSUInteger index; |
| 13 | +@property (nonatomic, assign) NSTimeInterval duration; |
| 14 | + |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation RCTGIFCoderFrame |
| 18 | +@end |
| 19 | + |
| 20 | +@implementation RCTAnimatedImage { |
| 21 | + CGImageSourceRef _imageSource; |
| 22 | + CGFloat _scale; |
| 23 | + NSUInteger _loopCount; |
| 24 | + NSUInteger _frameCount; |
| 25 | + NSArray<RCTGIFCoderFrame *> *_frames; |
| 26 | +} |
| 27 | + |
| 28 | +- (instancetype)initWithData:(NSData *)data scale:(CGFloat)scale |
| 29 | +{ |
| 30 | + if (self = [super init]) { |
| 31 | + CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); |
| 32 | + if (!imageSource) { |
| 33 | + return nil; |
| 34 | + } |
| 35 | + |
| 36 | + BOOL framesValid = [self scanAndCheckFramesValidWithSource:imageSource]; |
| 37 | + if (!framesValid) { |
| 38 | + CFRelease(imageSource); |
| 39 | + return nil; |
| 40 | + } |
| 41 | + |
| 42 | + _imageSource = imageSource; |
| 43 | + |
| 44 | + // grab image at the first index |
| 45 | + UIImage *image = [self animatedImageFrameAtIndex:0]; |
| 46 | + if (!image) { |
| 47 | + return nil; |
| 48 | + } |
| 49 | + self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:image.imageOrientation]; |
| 50 | + |
| 51 | + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; |
| 52 | + } |
| 53 | + |
| 54 | + return self; |
| 55 | +} |
| 56 | + |
| 57 | +- (BOOL)scanAndCheckFramesValidWithSource:(CGImageSourceRef)imageSource |
| 58 | +{ |
| 59 | + if (!imageSource) { |
| 60 | + return NO; |
| 61 | + } |
| 62 | + NSUInteger frameCount = CGImageSourceGetCount(imageSource); |
| 63 | + NSUInteger loopCount = [self imageLoopCountWithSource:imageSource]; |
| 64 | + NSMutableArray<RCTGIFCoderFrame *> *frames = [NSMutableArray array]; |
| 65 | + |
| 66 | + for (size_t i = 0; i < frameCount; i++) { |
| 67 | + RCTGIFCoderFrame *frame = [[RCTGIFCoderFrame alloc] init]; |
| 68 | + frame.index = i; |
| 69 | + frame.duration = [self frameDurationAtIndex:i source:imageSource]; |
| 70 | + [frames addObject:frame]; |
| 71 | + } |
| 72 | + |
| 73 | + _frameCount = frameCount; |
| 74 | + _loopCount = loopCount; |
| 75 | + _frames = [frames copy]; |
| 76 | + |
| 77 | + return YES; |
| 78 | +} |
| 79 | + |
| 80 | +- (NSUInteger)imageLoopCountWithSource:(CGImageSourceRef)source |
| 81 | +{ |
| 82 | + NSUInteger loopCount = 1; |
| 83 | + NSDictionary *imageProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(source, nil); |
| 84 | + NSDictionary *gifProperties = imageProperties[(__bridge NSString *)kCGImagePropertyGIFDictionary]; |
| 85 | + if (gifProperties) { |
| 86 | + NSNumber *gifLoopCount = gifProperties[(__bridge NSString *)kCGImagePropertyGIFLoopCount]; |
| 87 | + if (gifLoopCount != nil) { |
| 88 | + loopCount = gifLoopCount.unsignedIntegerValue; |
| 89 | + } |
| 90 | + } |
| 91 | + return loopCount; |
| 92 | +} |
| 93 | + |
| 94 | +- (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source |
| 95 | +{ |
| 96 | + float frameDuration = 0.1f; |
| 97 | + CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil); |
| 98 | + if (!cfFrameProperties) { |
| 99 | + return frameDuration; |
| 100 | + } |
| 101 | + NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties; |
| 102 | + NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary]; |
| 103 | + |
| 104 | + NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime]; |
| 105 | + if (delayTimeUnclampedProp != nil) { |
| 106 | + frameDuration = [delayTimeUnclampedProp floatValue]; |
| 107 | + } else { |
| 108 | + NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime]; |
| 109 | + if (delayTimeProp != nil) { |
| 110 | + frameDuration = [delayTimeProp floatValue]; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + CFRelease(cfFrameProperties); |
| 115 | + return frameDuration; |
| 116 | +} |
| 117 | + |
| 118 | +- (NSUInteger)animatedImageLoopCount |
| 119 | +{ |
| 120 | + return _loopCount; |
| 121 | +} |
| 122 | + |
| 123 | +- (NSUInteger)animatedImageFrameCount |
| 124 | +{ |
| 125 | + return _frameCount; |
| 126 | +} |
| 127 | + |
| 128 | +- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index |
| 129 | +{ |
| 130 | + if (index >= _frameCount) { |
| 131 | + return 0; |
| 132 | + } |
| 133 | + return _frames[index].duration; |
| 134 | +} |
| 135 | + |
| 136 | +- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index |
| 137 | +{ |
| 138 | + CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL); |
| 139 | + if (!imageRef) { |
| 140 | + return nil; |
| 141 | + } |
| 142 | + UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:_scale orientation:UIImageOrientationUp]; |
| 143 | + CGImageRelease(imageRef); |
| 144 | + return image; |
| 145 | +} |
| 146 | + |
| 147 | +- (void)didReceiveMemoryWarning:(NSNotification *)notification |
| 148 | +{ |
| 149 | + if (_imageSource) { |
| 150 | + for (size_t i = 0; i < _frameCount; i++) { |
| 151 | + CGImageSourceRemoveCacheAtIndex(_imageSource, i); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +- (void)dealloc |
| 157 | +{ |
| 158 | + if (_imageSource) { |
| 159 | + CFRelease(_imageSource); |
| 160 | + _imageSource = NULL; |
| 161 | + } |
| 162 | + [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; |
| 163 | +} |
| 164 | + |
| 165 | +@end |
0 commit comments