/** Gif的步驟 1. 拿到Gifd的數據 2. 將Gif分解爲一幀幀 3. 將單幀數據轉爲UIImage 4. 單幀圖片保存 */
github地址: https://github.com/mancongiOS/UIImage.gitgit
導入:github
#import <ImageIO/ImageIO.h> // 圖像的輸入輸出文件 #import <MobileCoreServices/MobileCoreServices.h>
實現: atom
- (void)didCompositionGif { //1. 拿到gif數據 NSString * gifPathSource = [[NSBundle mainBundle] pathForResource:@"kobe" ofType:@"gif"]; NSData * data = [NSData dataWithContentsOfFile:gifPathSource]; #warning 橋接的意義 (__bridge CFDataRef) CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); //2. 將gif分解爲一幀幀 size_t count = CGImageSourceGetCount(source); NSLog(@"%zu",count); NSMutableArray * tmpArray = [NSMutableArray arrayWithCapacity:0]; for (int i = 0; i < count; i ++) { CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL); //3. 將單幀數據轉爲UIImage UIImage * image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; [tmpArray addObject:image]; #warning CG類型的對象 不能用ARC自動釋放內存.須要手動釋放 CGImageRelease(imageRef); } CFRelease(source); // 單幀圖片保存 int i = 0; for (UIImage * image in tmpArray) { i ++; NSData * data = UIImagePNGRepresentation(image); NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * gifPath = path[0]; NSLog(@"gifPath: %@",gifPath); NSString * pathNum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]]; [data writeToFile:pathNum atomically:NO]; } }