首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生成的Gif太大- ObjectiveC

生成的Gif太大- ObjectiveC
EN

Stack Overflow用户
提问于 2017-11-27 04:05:51
回答 1查看 129关注 0票数 0

我使用下面的方法生成gif。

代码语言:javascript
复制
-(void)makeAnimatedGif:(NSArray *)imgArray {
    __block NSString *fileName = [NSString stringWithFormat:@"%ld",(long)[[NSDate date] timeIntervalSince1970]];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Do background work
        NSUInteger kFrameCount = imgArray.count;

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

        NSDictionary *frameProperties = @{
                                          (__bridge id)kCGImagePropertyGIFDictionary: @{
                                                  (__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // 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:fileName];

        CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL);
        CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);

        for (NSUInteger i = 0; i < kFrameCount; i++) {
            @autoreleasepool {
                UIImage *image =[imgArray objectAtIndex:i];  //Here is the change
                if (image.CGImage) {
                    CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
                }
            }
        }

        if (!CGImageDestinationFinalize(destination)) {
            NSLog(@"failed to finalize image destination");
        }

        dispatch_async(dispatch_get_main_queue(), ^{
            //Update UI
            CFRelease(destination);
            [self manageGifWithUrl:fileURL];
        });
    });

}

我的gif的问题是尺寸太大了。生成的gif大小范围为40MB - 90MB。我找不到任何解决方案。

我的imgArray包含180张图片。有什么办法解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2017-11-28 08:30:17

如果要创建视频,请对视频进行编码。动画GIF对于像动画这样的视频封装来说是很糟糕的。它们之所以变得如此普遍,是因为每个浏览器都可以呈现它们。除非你的目标是跨平台的最小公分母回放,否则不要使用gif动画。

40-80MB听起来并不奇怪。要减小大小,请降低输入图像的分辨率--通过简单地使其更小或通过减小调色板以使其压缩得更好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47500506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档