Skip to content

Commit ec3f082

Browse files
tianling536sd
authored andcommitted
Update DBAWebpImageDecoder.m
1 parent 7c644b1 commit ec3f082

File tree

1 file changed

+12
-72
lines changed

1 file changed

+12
-72
lines changed

DBAWebpImageDecoder.m

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -34,78 +34,18 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData
3434
UIImage *image;
3535
WebPBitstreamFeatures features;
3636
WebPGetFeatures([imageData bytes], [imageData length], &features);
37-
if (features.has_animation) {
38-
int width = features.width;
39-
int height = features.height;
40-
41-
WebPData webp_data;
42-
const uint8_t* data = [imageData bytes];
43-
size_t size = [imageData length];
44-
webp_data.bytes = data;
45-
webp_data.size = size;
46-
47-
WebPAnimDecoderOptions dec_options;
48-
WebPAnimDecoderOptionsInit(&dec_options);
49-
dec_options.color_mode = MODE_RGBA;
50-
51-
WebPAnimDecoder* dec = WebPAnimDecoderNew(&webp_data, &dec_options);
52-
WebPAnimInfo anim_info;
53-
WebPAnimDecoderGetInfo(dec, &anim_info);
54-
int timestamp = 0;
55-
NSMutableArray<NSNumber *> *delays = [NSMutableArray arrayWithCapacity:anim_info.frame_count];
56-
NSMutableArray<id /* CGIMageRef */> *images = [NSMutableArray arrayWithCapacity:anim_info.frame_count];
57-
int frameIndex = 0;
58-
59-
while (WebPAnimDecoderHasMoreFrames(dec)) {
60-
uint8_t* frame_rgba;
61-
uint8_t* curr_rgba = malloc(width * 4 * height);
62-
memset(curr_rgba, 0, width * 4 * height);
63-
int prevTimestamp = timestamp;
64-
WebPAnimDecoderGetNext(dec, &frame_rgba, &timestamp);
65-
66-
memcpy(curr_rgba, frame_rgba, width * 4 * height);
67-
int delay = timestamp - prevTimestamp;
68-
delays[frameIndex] = [NSNumber numberWithInt:delay];
69-
70-
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, curr_rgba, width*height*4, free_data);
71-
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent);
72-
images[frameIndex] = (__bridge_transfer id)imageRef;
73-
74-
frameIndex++;
75-
CGDataProviderRelease(provider);
76-
}
77-
image = [UIImage imageWithCGImage:(__bridge CGImageRef _Nonnull)(images[0]) scale:scale orientation:UIImageOrientationUp];
78-
79-
NSMutableArray<NSNumber *> *keyTimes = [NSMutableArray arrayWithCapacity:delays.count];
80-
NSTimeInterval runningDuration = 0;
81-
for (NSNumber *delayNumber in delays) {
82-
[keyTimes addObject:@(runningDuration / timestamp)];
83-
runningDuration += delayNumber.doubleValue;
84-
}
85-
[keyTimes addObject:@1.0];
86-
87-
WebPAnimDecoderDelete(dec);
88-
89-
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
90-
animation.calculationMode = kCAAnimationDiscrete;
91-
animation.repeatCount = anim_info.loop_count == 0 ? HUGE_VALF : anim_info.loop_count;
92-
animation.keyTimes = keyTimes;
93-
animation.values = images;
94-
animation.duration = timestamp / 1000.0;
95-
animation.removedOnCompletion = NO;
96-
image.reactKeyframeAnimation = animation;
97-
} else {
98-
int width = 0, height = 0;
99-
uint8_t *data = WebPDecodeRGBA([imageData bytes], [imageData length], &width, &height);
100-
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, width*height*4, free_data);
101-
102-
103-
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent);
104-
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
105-
106-
CGDataProviderRelease(provider);
107-
CGImageRelease(imageRef);
108-
}
37+
38+
int width = 0, height = 0;
39+
uint8_t *data = WebPDecodeRGBA([imageData bytes], [imageData length], &width, &height);
40+
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, width*height*4, free_data);
41+
42+
43+
CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent);
44+
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
45+
46+
CGDataProviderRelease(provider);
47+
CGImageRelease(imageRef);
48+
10949
CGColorSpaceRelease(colorSpaceRef);
11050
completionHandler(nil, image);
11151
return ^{};

0 commit comments

Comments
 (0)