iOS学习笔记--gif图片分解
程序员文章站
2022-03-24 22:21:46
...
gif图片分解的过程可以分为以下四步:
1.获取gif图片的数据
2.将gif图片分解成帧
3.将单帧数据转化为UIimage
4.单帧图片保存
在这之前需要在工程中添加framework:ImageIO和MobileCoreServices
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
-(void)gifPictureDecomposition:(NSString*)pictureName
{
//1.获取gif图片数据
//将图片数据转化为data
NSData * data = [NSData dataWithContentsOfFile:gifpathsource];
CGImageSourceRef sourec = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
//2.将gif图片分解成帧
size_t count = CGImageSourceGetCount(sourec);
//定义数组保存单帧图片数据
self.tmpArr = [[NSMutableArray alloc]init];
for (size_t i = 0; i < count; i++)
{
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(sourec, i, NULL);
//3.将单帧图片转化为UIimage
UIImage * image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
[self.tmpArr addObject:image];
//释放imageRef
CGImageRelease(imageRef);
}
//释放sourec
CFRelease(sourec);
//4.单帧图片保存
int i = 0;
for (UIImage * image in self.tmpArr)
{
NSData * data = UIImagePNGRepresentation(image);
NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString * gifpath = path[0];
NSString * pathNum = [gifpath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
i++;
[data writeToFile:pathNum atomically:NO];
NSLog(@"%@",path);//图片存储的路径
}
}