gif图片分解的过程可以分为以下四步:
1.获取gif图片的数据
2.将gif图片分解成帧
3.将单帧数据转化为UIimage
4.单帧图片保存
在这之前需要在工程中添加framework:ImageIO和MobileCoreServices
1 2
| #import <ImageIO/ImageIO.h> #import <MobileCoreServices/MobileCoreServices.h>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| -(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);//图片存储的路径 } }
|