本文介绍了如何在iOS中生成24位真彩色动画GIF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从几个表示为base64字符串的PNG文件中生成一个真彩色动画Gif。我找到了,见第23节)。你还需要找一个LZW压缩机,这个压缩机并不难找。动画还将使用一个淫秽的存储空间:包括base64转换,我估计大约43位/像素,或720p视频大约1.2 Gbit / s,这是你用于高质量MPEG4的存储量的400倍或者WebM,可能是PNG所需存储量的3倍。存储和带宽要求可能会给主机和客户带来不必要的成本,除非动画非常短且很小。



请注意,这不允许您使用Alpha透明度,这是GIF格式的一个严格限制。



意见



将高质量动画放入的想法GIF在极端情况下是荒谬的,即使它是可能的。鉴于可用的替代方案,这是特别荒谬的:




  • 如果您的目标是现代浏览器或移动设备,MPEG4()和WebM()是显而易见的选择。在两种格式之间,只有Opera Mini不支持。


  • 如果您的目标是较旧的浏览器或功能较少的设备,或者您无法负担MPEG4编码,那么可以将帧编码为单独的JPEG或PNG图像。使用时序将这些与JSON有效负载捆绑在一起,并使用JavaScript或其他客户端脚本在动画帧之间切换。这非常有效。




注释



来自:


I want to generate a true color animated Gif from a couple of PNG files represented as base64 string. I found this post and did something similar. I have an array with the dataUrls:

NSArray* imageDataUrls; // array with the data urls without data:image/png;base64, prefix

Here is what I did:

  NSDictionary *fileProperties = @{
                                     (__bridge id)kCGImagePropertyGIFDictionary: @{
                                             (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
                                             }
                                     };


    NSDictionary *frameProperties = @{
                                      (__bridge id)kCGImagePropertyGIFDictionary: @{
                                              (__bridge id)kCGImagePropertyGIFDelayTime: @0.4f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
                                              }
                                      };


        NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
        NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];


        CFMutableDataRef destinationData = CFDataCreateMutable(kCFAllocatorDefault, 0);
        CGImageDestinationRef destination = CGImageDestinationCreateWithData(destinationData, kUTTypeGIF, kFrameCount, NULL);


        CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);



        NSData* myImageData;
        UIImage *myImage = [UIImage alloc];
        for (NSUInteger i = 0; i < kFrameCount; i++) {
            @autoreleasepool {
                myImageData = [NSData dataFromBase64String:[imageDataUrls objectAtIndex:i]];
                myImage = [myImage initWithData: myImageData];

                CGImageDestinationAddImage(destination, myImage.CGImage, (__bridge CFDictionaryRef)frameProperties);
            }
        }
        myImageData = nil;
        myImage = nil;


CFRelease(destination);

NSData* data = nil;
data = (__bridge NSData *)destinationData;

Finally, I send the gif image as base64EncodedString back to the phonegap container.

// send back gif image
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: [data base64EncodedString]];

It works good but the quality of the resulting gif image is bad. This is because it has only 256 colors.

Here is the original png image:

Here is a screenshot of the generated gif image:

How do I get the same quality as I imported, i.e., how can I raise the quality level of the generated gif? How do I generate true color gifs on iOS?

解决方案

GIFs are not designed to store true-color data, and they are also poorly suited for animations. Since this is such an unusual use of GIFs, you will have to write a lot of your own code.

  1. Break each frame into rectangular chunks, where each chunk contains at most 256 distinct colors. The easiest way to do this is to use 16x16 chunks.

  2. Convert each chunk to an indexed image.

  3. Add each chunk to the GIF. For the first chunk in a frame, use the frame delay. For other chunks in a frame, use a delay of 0.

Done. You will have to familiarize yourself with the GIF specification, which is freely available online (GIF89a specification at W3.org, see section 23). You will also need to find an LZW compressor, which is not too hard to find. The animation will also use an obscene amount of storage: including base64 conversion, I estimate about 43 bits/pixel, or about 1.2 Gbit/s for 720p video, which is about 400x as much storage as you would use for high-quality MPEG4 or WebM, and probably about 3x as much storage as the PNGs would require. The storage and bandwidth requirements will likely incur undesirable costs for hosts and clients, unless the animations are very short and small.

Note that this will not allow you to use alpha transparency, this is a hard limitation of the GIF format.

Opinion

The idea of putting high quality animations in a GIF is absurd in the extreme, even though it is possible. It is especially absurd given the available alternatives:

  • If you are targeting modern browsers or mobile devices, MPEG4 (support matrix) and WebM (support matrix) are the obvious choices. Between the two formats, only Opera Mini supports neither.

  • If you are targeting older browsers or less-capable devices, or if you cannot afford MPEG4 encoding, you can encode the frames as individual JPEG or PNG images. Bundle these with a JSON payload with the timing, and use JavaScript or other client-side scripting to switch between animation frames. This works surprisingly well.

Notes

From the GIF 89a specification:

这篇关于如何在iOS中生成24位真彩色动画GIF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:05