码迷,mamicode.com
首页 > 编程语言 > 详细

将GIF图转化为UIImageView可展示的动画数组

时间:2015-08-08 16:13:15      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

如何将gif转化为图片数组,ios开发

 

//引入头文件
#import <ImageIO/ImageIO.h>

- (NSMutableArray *)loadGifArray
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"pross" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    //目标数组
    NSMutableArray *images = [self praseGIFDataToImageArray:data];

    return images;
}

//gif转化为imageView动画数组
- (NSMutableArray *)praseGIFDataToImageArray:(NSData *)data;
{
    NSMutableArray *frames = [[NSMutableArray alloc] init];
    CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)data, NULL);
    CGFloat animationTime = 0.f;
    if (src) {
        size_t l = CGImageSourceGetCount(src);
        frames = [NSMutableArray arrayWithCapacity:l];
        for (size_t i = 0; i < l; i++) {
            CGImageRef img = CGImageSourceCreateImageAtIndex(src, i, NULL);
            NSDictionary *properties = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(src, i, NULL));
            NSDictionary *frameProperties = [properties objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
            NSNumber *delayTime = [frameProperties objectForKey:(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
            animationTime += [delayTime floatValue];
            if (img) {
                [frames addObject:[UIImage imageWithCGImage:img]];
                CGImageRelease(img);
            }
        }
        CFRelease(src);
    }
    return frames;
}

 

将GIF图转化为UIImageView可展示的动画数组

标签:

原文地址:http://www.cnblogs.com/qiulilin/p/4713124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!