根据一张gif图返回每张图片

Jan 10, 2017

由于GPUImage的UIElement在processFrame的block中回调需要用到过渡图片调用,这里处理是将gif图全部取出来,然后在里面进行更新
根据一篇博客http://blog.csdn.net/zafir_zzf/article/details/52993853
有以下操作:

  1. 获取到GIF图的源数据 CGImageSourceRef
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
  2. 由源数据 SourceRef生成一张CGImageRef类型图片
    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
  3. 拿到里面的图片数
    size_t gifCount = CGImageSourceGetCount(gifSource);
  4. 有CGImageRef生成UIImage ,我们想要的.
    UIImage *image = [UIImage imageWithCGImage:imageRef];
  5. 最后别忘了给他释放掉
    CGImageRelease(imageRef);
    他这里提供了一段代码,记得,需要添加Binary:ImageIO
    `#import <ImageIO/ImageIO.h>
    @implementation NSArray (CDIExtention)
  • (NSArray )cdi_imagesWithGif:(NSString )gifNameInBoundle {
    NSURL fileUrl = [[NSBundle mainBundle] URLForResource:gifNameInBoundle withExtension:@”gif”];
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
    size_t gifCount = CGImageSourceGetCount(gifSource);
    NSMutableArray
    frames = [[NSMutableArray alloc]init];
    for (size_t i = 0; i< gifCount; i++) {
    CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    [frames addObject:image];
    CGImageRelease(imageRef);
    }
    return frames;
    }
    @end`