本文介绍了解码动画Webp iOS时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经努力了两天,以在 UIImageView 中显示动画的 webp 图像,但没有成功。

I've been struggling for two days to display an animated webp image in a UIImageView with no success whatsoever.

主要问题是在文件的解码步骤中出现以下错误: VP8_STATUS_UNSUPPORTED_FEATURE

Mainly the problem is in the decoding step of the file which gives this error: VP8_STATUS_UNSUPPORTED_FEATURE.

我尝试了

这些项目提供了用于创建的代码包含 webp 文件的UIImage ,它们对于没有动画的图像都可以正常工作,但是当尝试对带有动画的图像进行解码时,它们都失败,并出现与上述相同的错误。

These projects provide code for creating UIImage with webp files and they work fine with images with no animation but they both fail with the same error as above when attempting to decode images with animation.

我越狱了,检查了文件系统,我发现Facebook的Messenger应用在 .webp 中有一些贴纸。格式以及许可证中,他们都提到了Google的 webp库,所以我确定可以实现。

I am jailbroken and checking the filesystem I saw that Facebook's Messenger app has some of it's stickers in .webp format and also in the License they mention Google's "webp" library so I'm sure somehow it's possible.

推荐答案

已成功解码一种使用还包含对使用的数据结构的解释。

Managed to decode animated .webp using the code snippet at the top of this header which also contains explanations of the data structures used.

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;
        }

这篇关于解码动画Webp iOS时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 21:30