Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DelayTime or UnclampedDelayTime for GIFS

When converting a Gif into a animated UIImage the delay for each frame is extracted from properties kCGImagePropertyGIFDelayTime or kCGImagePropertyGIFUnclampedDelayTime

Like so:

NSString *frameKeyPath = [NSString stringWithFormat:@"%@.%@",(NSString*)kCGImagePropertyGIFDictionary,kCGImagePropertyGIFUnclampedDelayTime];
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source,i,nil);
NSDictionary *frameProperties = (__bridge NSDictionary*)cfFrameProperties;
NSNumber *delayTimeProp = [frameProperties valueForKeyPath:frameKeyPath];

Chrome chooses the correct one in both example cases:

kCGImagePropertyGIFDelayTime = 0.1 : http://i.imgur.com/tX9cjUO.gif

kCGImagePropertyGIFUnclampedDelayTime = 0.01 : http://i.minus.com/iIOyK7SKp8TYc.gif

Choosing one for every case results in one of the example images animating too slow or too fast

Is there a way to determine which property to use?

Thanks

like image 287
amleszk Avatar asked Jun 06 '13 14:06

amleszk


1 Answers

Ended up using the webkit approach which chooses, unclamped, clamped or 0.1 default

+ (float)frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source
{
    float frameDuration = 0.1f;
    CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source,index,nil);
    NSDictionary *frameProperties = (__bridge NSDictionary*)cfFrameProperties;
    NSDictionary *gifProperties = frameProperties[(NSString*)kCGImagePropertyGIFDictionary];

    NSNumber *delayTimeUnclampedProp = gifProperties[(NSString*)kCGImagePropertyGIFUnclampedDelayTime];
    if(delayTimeUnclampedProp) {
        frameDuration = [delayTimeUnclampedProp floatValue];
    } else {

        NSNumber *delayTimeProp = gifProperties[(NSString*)kCGImagePropertyGIFDelayTime];
        if(delayTimeProp) {
            frameDuration = [delayTimeProp floatValue];
        }
    }

    // Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
    // We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
    // a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
    // for more information.

    if (frameDuration < 0.011f)
        frameDuration = 0.100f;

    CFRelease(cfFrameProperties);
    return frameDuration;
}
like image 144
amleszk Avatar answered Oct 30 '22 17:10

amleszk