objective-canimated-gifcgimagesource

kCGImagePropertyGIFLoopCount returns wrong value


I have three GIF files which are loaded as NSData. When I use the key kCGImagePropertyGIFLoopCount to retrieve the LoopCount, it returns a wrong value.
For the GIF which should loop infinitely, it returns 0 (correct).
For the GIF with Loop count 1, it returns 0 (incorrect).
For the GIF with Loop count 2, it returns 1 (incorrect).

-(void)prepareGif:(NSData*)data {
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) data, NULL);
CFDictionaryRef properties = CGImageSourceCopyProperties(source, nil);
NSDictionary* imageProperties = (__bridge NSDictionary*) properties;
NSDictionary* gifProperty = imageProperties[(NSDictionary*) kCGImagePropertyGIFDictionary];
int loopCount = [gifProperty[(NSString*) kCGImagePropertyGIFLoopCount] intValue]; // returns 0 for LoopCount 1 and LoopCount 0 and returns 1 for LoopCount 2

Here are the three GIFs:
loopTwice loopOnce infiniteLoop
Someone know what I am doing wrong here or is it a bug?


Solution

  • I found the answer. The property is absent when it doesnt loop (i.e. playback count = 1).
    It was my mistake since the gifProperty dictionary returns objects and not primitive types. The returned object was a NSNumber, which was casted to int with a default value of 0. I didn't check for nil