相关文章推荐
淡定的可乐  ·  关于公布周军伟等1518位同志具有中、初级专 ...·  1 年前    · 
飘逸的小刀  ·  抗戰勝利70周年紀念大會 習近平發表重要講話 ...·  1 年前    · 
骑白马的葡萄  ·  关于印发《湖南省公路水运工程建设领域农民工工 ...·  2 年前    · 
小眼睛的葡萄酒  ·  【MAUI】为 Label、Image ...·  2 年前    · 
活泼的签字笔  ·  关于提升农村产业链现代会水平的建议-南通市通 ...·  2 年前    · 
小百科  ›  iOS 获取gif图片循环次数和时长开发者社区
ios
冷静的葫芦
3 年前
作者头像
且行且珍惜_iOS
0 篇文章

iOS 获取gif图片循环次数和时长

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > iOS开发攻城狮的集散地 > iOS 获取gif图片循环次数和时长

iOS 获取gif图片循环次数和时长

作者头像
且行且珍惜_iOS
发布 于 2018-05-22 17:16:29
2.4K 1
发布 于 2018-05-22 17:16:29
举报
//获取gif图片的总时长和循环次数
- (NSTimeInterval)durationForGifData:(NSData *)data{ 
    //将GIF图片转换成对应的图片源
    CGImageSourceRef gifSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    //获取其中图片源个数,即由多少帧图片组成
    size_t frameCout = CGImageSourceGetCount(gifSource);
    //定义数组存储拆分出来的图片
    NSMutableArray* frames = [[NSMutableArray alloc] init];
    NSTimeInterval totalDuration = 0;
    for (size_t i=0; i<frameCout; i++) {
        //从GIF图片中取出源图片
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        //将图片源转换成UIimageView能使用的图片源
        UIImage* imageName = [UIImage imageWithCGImage:imageRef];
        //将图片加入数组中
        [frames addObject:imageName];
        NSTimeInterval duration = [self gifImageDeleyTime:gifSource index:i];
        totalDuration += duration;
        CGImageRelease(imageRef);
   //获取循环次数   
    NSInteger loopCount;//循环次数
    CFDictionaryRef properties = CGImageSourceCopyProperties(gifSource, NULL);
    if (properties) {
        CFDictionaryRef gif = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
        if (gif) {
            CFTypeRef loop = CFDictionaryGetValue(gif, kCGImagePropertyGIFLoopCount);
            if (loop) {
         //如果loop == NULL,表示不循环播放,当loopCount  == 0时,表示无限循环;
           CFNumberGetValue(loop, kCFNumberNSIntegerType, &loopCount)
    CFRelease(gifSource);
    return totalDuration;
}

循环次数的键:kCGImagePropertyGIFLoopCount

无限循环播放.png

不循环播放.png

时间间隔的键:kCGImagePropertyGIFUnclampedDelayTime

每一帧时间间隔.png

 //获取GIF图片每帧的时长
- (NSTimeInterval)gifImageDeleyTime:(CGImageSourceRef)imageSource index:(NSInteger)index {
    NSTimeInterval duration = 0;
    CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, index, NULL);
    if (imageProperties) {
        CFDictionaryRef gifProperties;
        BOOL result = CFDictionaryGetValueIfPresent(imageProperties, kCGImagePropertyGIFDictionary, (const void **)&gifProperties);
        if (result) {
            const void *durationValue;
            if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFUnclampedDelayTime, &durationValue)) {
                duration = [(__bridge NSNumber *)durationValue doubleValue];
                if (duration < 0) {
                    if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFDelayTime, &durationValue)) {
                        duration = [(__bridge NSNumber *)durationValue doubleValue];
 
推荐文章
淡定的可乐  ·  关于公布周军伟等1518位同志具有中、初级专业技术资格的通知(嘉人社〔2025〕6号)(2024年南湖、秀洲、海盐教师中级,普通工程中、初级)
1 年前
飘逸的小刀  ·  抗戰勝利70周年紀念大會 習近平發表重要講話(全文)--時政--人民網
1 年前
骑白马的葡萄  ·  关于印发《湖南省公路水运工程建设领域农民工工资支付管理监督实施细则》的通知 - 湖南省交通运输厅
2 年前
小眼睛的葡萄酒  ·  【MAUI】为 Label、Image 等控件添加点击事件 - leslie_xin - 博客园
2 年前
活泼的签字笔  ·  关于提升农村产业链现代会水平的建议-南通市通州区人民政府
2 年前
Link管理   ·   Sov5搜索   ·   小百科
小百科 - 百科知识指南