你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中GIF的制作的簡單版本記錄

iOS中GIF的制作的簡單版本記錄

編輯:IOS開發綜合

之前做過一個關於gif的app,一直沒有來得及記錄,現在記錄一下學習的過程.

制作gif是基於ImageIO.framework,所以要先添加這個庫

 

gif分解圖片

 

 


- (void)viewDidLoad
{
    [super viewDidLoad];

    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@test101 ofType:@gif]];

    //通過data獲取image的數據源
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    
    //獲取幀數
    size_t count = CGImageSourceGetCount(source);
    
    NSMutableArray* tmpArray = [NSMutableArray array];
    for (size_t i = 0; i < count; i++)
    {
        //獲取圖像
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
        
        //生成image
        UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        
        [tmpArray addObject:image];
        
        CGImageRelease(imageRef);
    }
    CFRelease(source);
    
    int i = 0;
    for (UIImage *img in tmpArray) {
        //寫文件
        NSData *imageData = UIImagePNGRepresentation(img);
        
        NSString *pathNum = [[self backPath] stringByAppendingPathComponent:[NSString stringWithFormat:@%d.png,i]];
        [imageData writeToFile:pathNum atomically:NO];
        i++;
    }
       
}

//返回保存圖片的路徑
-(NSString *)backPath{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *path = [UniversalMethod backDocumentDirectoryPath];
    
    NSString *imageDirectory = [path stringByAppendingPathComponent:@Normal];
    
    [fileManager createDirectoryAtPath:imageDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    return imageDirectory;
}

 

gif的制作

制作gif需要依賴MobileCoreServices.framework

 

 

//gif的制作
    
    //獲取源數據image
    NSMutableArray *imgs = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@bear_1],[UIImage imageNamed:@bear_2], nil];
    
    //圖像目標
    CGImageDestinationRef destination;
    
    //創建輸出路徑
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentStr = [document objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *textDirectory = [documentStr stringByAppendingPathComponent:@gif];
    [fileManager createDirectoryAtPath:textDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [textDirectory stringByAppendingPathComponent:@test101.gif];
    
    
    NSLog(@%@,path);
    
    //創建CFURL對象
    /*
     CFURLCreateWithFileSystemPath(CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle pathStyle, Boolean isDirectory)
     
     allocator : 分配器,通常使用kCFAllocatorDefault
     filePath : 路徑
     pathStyle : 路徑風格,我們就填寫kCFURLPOSIXPathStyle 更多請打問號自己進去幫助看
     isDirectory : 一個布爾值,用於指定是否filePath被當作一個目錄路徑解決時相對路徑組件
     */
    CFURLRef url = CFURLCreateWithFileSystemPath (
                                                  kCFAllocatorDefault,
                                                  (CFStringRef)path,
                                                  kCFURLPOSIXPathStyle,
                                                  false);
    
    //通過一個url返回圖像目標
    destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imgs.count, NULL);
    
    //設置gif的信息,播放間隔時間,基本數據,和delay時間
    NSDictionary *frameProperties = [NSDictionary
                                     dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3], (NSString *)kCGImagePropertyGIFDelayTime, nil]
                                     forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    //設置gif信息
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
    
    [dict setObject:[NSNumber numberWithBool:YES] forKey:(NSString*)kCGImagePropertyGIFHasGlobalColorMap];
    
    [dict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
    
    [dict setObject:[NSNumber numberWithInt:8] forKey:(NSString*)kCGImagePropertyDepth];
    
    [dict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
    NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:dict
                                                              forKey:(NSString *)kCGImagePropertyGIFDictionary];
    //合成gif
    for (UIImage* dImg in imgs)
    {
        CGImageDestinationAddImage(destination, dImg.CGImage, (__bridge CFDictionaryRef)frameProperties);
    }
    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved