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);
有帮助吗?

解决方案

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

其他提示

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

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top