有没有办法对生成的GIF进行无损压缩?

时间:2012-08-10 16:40:38

标签: ios gif lossless-compression

我们使用以下代码从一组JPEG图像生成GIF文件,对于进行无损压缩的设置,它似乎根本不会生成较小的文件。我们在这里做对了吗?

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

3 个答案:

答案 0 :(得分:3)

GIF不支持kCGImageDestinationLossyCompressionQuality属性。据我所知,没有内置支持在iOS上压缩GIF - 我无法使色彩图工作。

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

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

答案 1 :(得分:1)

GIF图像格式是无损压缩。但是,您正在压缩(有损)压缩格式。文件大小可能会增加。

答案 2 :(得分:0)

Jpeg图像包含许多非常相似但不相同的像素,这些像素非常难以压缩无损压缩方案。要获得更好的压缩效果,您必须先对颜色进行量化。在您丢失信息以使图像可压缩之后,Gif图像将无损失。