解码动画 webp 时出错 iOS

Error decoding animated webp iOS

我已经为在 UIImageView 中显示动画 webp 图像而苦苦挣扎了两天,但没有成功。

主要问题出在文件的解码步骤中,给出了这个错误:VP8_STATUS_UNSUPPORTED_FEATURE

我试过了

https://github.com/seanooi/iOS-WebP

https://github.com/mattt/WebPImageSerialization

这些项目提供了使用 webp 文件创建 UIImage 的代码,它们可以很好地处理没有动画的图像,但是当尝试解码带有动画的图像时,它们都失败并出现与上述相同的错误。

我正在越狱并检查文件系统我看到 Facebook 的 Messenger 应用程序有一些 .webp 格式的贴纸,并且在许可证中他们提到了 Google 的 "webp" 库所以我确定这是可能的。

设法使用 this header 顶部的代码片段解码动画 .webp,其中还包含对所用数据结构的解释。

static NSDictionary* DecodeWebPURL(NSURL *url) {

            NSMutableDictionary *info = [NSMutableDictionary dictionary];
            NSMutableArray *images = [NSMutableArray array];
            NSData *imgData = [NSData dataWithContentsOfURL:url];

            WebPData data;
            WebPDataInit(&data);

            data.bytes = (const uint8_t *)[imgData bytes];
            data.size = [imgData length];

            WebPDemuxer* demux = WebPDemux(&data);

            int width = WebPDemuxGetI(demux, WEBP_FF_CANVAS_WIDTH);
            int height = WebPDemuxGetI(demux, WEBP_FF_CANVAS_HEIGHT);
            uint32_t flags = WebPDemuxGetI(demux, WEBP_FF_FORMAT_FLAGS);

            if (flags & ANIMATION_FLAG) {
                WebPIterator iter;
                if (WebPDemuxGetFrame(demux, 1, &iter)) {

                    WebPDecoderConfig config;
                    WebPInitDecoderConfig(&config);

                    config.input.height = height;
                    config.input.width = width;
                    config.input.has_alpha = iter.has_alpha;
                    config.input.has_animation = 1;
                    config.options.no_fancy_upsampling = 1;
                    config.options.bypass_filtering = 1;
                    config.options.use_threads = 1;
                    config.output.colorspace = MODE_RGBA;

                    [info setObject:[NSNumber numberWithInt:iter.duration] forKey:@"duration"];

                    do {
                        WebPData frame = iter.fragment;

                        VP8StatusCode status = WebPDecode(frame.bytes, frame.size, &config);
                        if (status != VP8_STATUS_OK) {
                            NSLog(@"Error decoding frame");
                        }

                        uint8_t *data = WebPDecodeRGBA(frame.bytes, frame.size, &width, &height);
                        CGDataProviderRef provider = CGDataProviderCreateWithData(&config, data, config.options.scaled_width  * config.options.scaled_height * 4, NULL);

                        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
                        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaLast;
                        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

                        CGImageRef imageRef = CGImageCreate(width, height, 8, 32, 4 * width, colorSpaceRef, bitmapInfo, provider, NULL, YES, renderingIntent);
                        [images addObject:[UIImage imageWithCGImage:imageRef]];
                        CGImageRelease(imageRef);
                        CGColorSpaceRelease(colorSpaceRef);
                        CGDataProviderRelease(provider);

                    } while (WebPDemuxNextFrame(&iter));

                    WebPDemuxReleaseIterator(&iter);
                }
            }
            WebPDemuxDelete(demux);
            [info setObject:images forKey:@"frames"];

            return info;
        }