Is there a way to do lossless compression for generated GIFs?

China☆狼群 提交于 2019-12-11 11:36:16

问题


We are using the following code to generate GIF file from a set of JPEG images, for the setting on doing lossless compression, it doesn't seem to generate a smaller sized file at all. Are we doing the right thing here?

CGImageDestinationRef imageDestination = CGImageDestinationCreateWithURL((CFURLRef)pathUrl, CFSTR("com.compuserve.gif"), images.count, NULL);

// image/frame level properties
NSDictionary *imageProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithFloat:delayTime], (NSString *)kCGImagePropertyGIFDelayTime,
                                 nil];
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                            imageProperties, (NSString *)kCGImagePropertyGIFDictionary, 
                            nil];

for (UIImage *image in [images objectEnumerator]) {
    CGImageDestinationAddImage(imageDestination, image.CGImage, (CFDictionaryRef)properties);
}

// gif level properties
NSDictionary *gifProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithInt:0], (NSString *)kCGImagePropertyGIFLoopCount,
                               [NSNumber numberWithInt:1.0], kCGImageDestinationLossyCompressionQuality,
                               nil];
properties = [NSDictionary dictionaryWithObjectsAndKeys:
              gifProperties, (NSString *)kCGImagePropertyGIFDictionary,
              nil];
CGImageDestinationSetProperties(imageDestination, (CFDictionaryRef)properties);
CGImageDestinationFinalize(imageDestination);
CFRelease(imageDestination);

回答1:


The GIF image format IS lossless compression. However you are compressing a (lossy) compressed format. File size may go up.




回答2:


GIF does not support the kCGImageDestinationLossyCompressionQuality property. No built in support for compressing gifs on iOS as far as I can tell -- I haven't been able to get the color map to work.

const CFStringRef kCGImagePropertyGIFLoopCount;
const CFStringRef kCGImagePropertyGIFDelayTime;
const CFStringRef kCGImagePropertyGIFImageColorMap;
const CFStringRef kCGImagePropertyGIFHasGlobalColorMap;
const CFStringRef kCGImagePropertyGIFUnclampedDelayTime;

Reference: https://developer.apple.com/library/ios/documentation/graphicsimaging/Reference/CGImageProperties_Reference/Reference/reference.html#//apple_ref/doc/constant_group/GIF_Dictionary_Keys




回答3:


Jpeg images contain many very similar but not identical pixels, which are very hard for lossless compression schemes to compress. To get better compression you have to quantize the colors first. Gif images will be lossless after you've taken the hit of losing information to make the image compressible.



来源:https://stackoverflow.com/questions/11905872/is-there-a-way-to-do-lossless-compression-for-generated-gifs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!