解析GIF(MMR项目中使用)

/**

 *  获取当前GIF的数组

 */

-(NSMutableArray*)imageArrayWithData:(NSData*)gifData{

    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)CFBridgingRetain(gifData), NULL);

    size_t imageCount = CGImageSourceGetCount(src);

    NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:imageCount];

    NSMutableArray *times = [[NSMutableArray alloc] initWithCapacity:imageCount];

    NSMutableArray *keyTimes = [[NSMutableArray alloc] initWithCapacity:imageCount];

    

    float totalTime = 0;

    for (size_t i = 0; i < imageCount; i++) {

        CGImageRef cgimage= CGImageSourceCreateImageAtIndex(src, i, NULL);

        [images addObject:(__bridge id)cgimage];

        CGImageRelease(cgimage);

        

        NSDictionary *properties = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(src, i, NULL);

        NSDictionary *gifProperties = [properties valueForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary];

        NSString *gifDelayTime = [gifProperties valueForKey:(__bridge NSString* )kCGImagePropertyGIFDelayTime];

        [times addObject:gifDelayTime];

        totalTime += [gifDelayTime floatValue];

    }

    

    float currentTime = 0;

    for (size_t i = 0; i < times.count; i++) {

        float keyTime = currentTime / totalTime;

        [keyTimes addObject:[NSNumber numberWithFloat:keyTime]];

        currentTime += [[times objectAtIndex:i] floatValue];

    }

    return images;

}

posted @ 2014-11-18 17:23  Shdow_Walker  阅读(237)  评论(0编辑  收藏  举报