1

我们正在使用以下代码从一组 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);
4

3 回答 3

4

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

于 2013-10-03T18:38:55.293 回答
1

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

于 2012-08-10T16:45:10.970 回答
0

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

于 2012-08-10T16:49:39.117 回答