/**
* 从Gif图片中获取图片数组
*
* @param resource gif图片名称,不需要携带Gif格式、完整地址等。有效格式示例:
* NSArray *gifArray = [self getImageFromGifResource:@"demo_gif_001"];
* 注意:GIF图片不要放在 .xcassets 资源文件夹下,否则加载不出来
* @return 指定大小的图片
*/
- (NSArray <UIImage *>*)getImageFromGifResource:(NSString *)resource {
NSMutableArray *imageArray = [NSMutableArray array];
// 获取gif url
NSURL *url = [[NSBundle mainBundle] URLForResource:resource withExtension:@"gif"];
// 转换为图片源
CGImageSourceRef gifImageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, nil);
// 获取图片个数
size_t framesCount = CGImageSourceGetCount(gifImageSourceRef);
for (size_t index = 0; index < framesCount; index ++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifImageSourceRef, index , nil);
UIImage *image = [UIImage imageWithCGImage:imageRef];
[imageArray addObject:image];
CFRelease(imageRef);
}
return imageArray;
}