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

主要问题在于文件的解码步骤中出现此错误:VP8_STATUS_UNSUPPORTED_FEATURE

我试过了

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

https://github.com/mattt/WebPImageSerialization

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

我越狱了,检查了文件系统,我发现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;
        }

关于ios - 解码动画Webp iOS时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28231580/

10-11 20:09