DelayTime or UnclampedDelayTime for GIFS

前端 未结 1 1990
独厮守ぢ
独厮守ぢ 2021-02-03 15:25

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

1条回答
  •  梦谈多话
    2021-02-03 16:11

    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  and 
        // for more information.
    
        if (frameDuration < 0.011f)
            frameDuration = 0.100f;
    
        CFRelease(cfFrameProperties);
        return frameDuration;
    }
    

    0 讨论(0)
提交回复
热议问题