iOS-GIF合成

叮叮咚咚鐺鐺,本領大


/**
 合成gif

 @param imagePathArray 圖片路徑數組
 */
- (void)composeGIF:(NSMutableArray *)imagePathArray {
    
    //創建gif路徑
    NSString *imagepPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString * gifPath = [imagepPath stringByAppendingString:@"/my.gif"];

    NSLog(@"gifPath   %@",gifPath);
    
    //圖像目標
    CGImageDestinationRef destination;

    CFURLRef url=CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)gifPath, kCFURLPOSIXPathStyle, false);
    
    //通過一個url返回圖像目標
    destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imagePathArray.count, NULL);
    
    //設置gif的信息,播放間隔時間,基本數據,和delay時間,可以自己設置
    NSDictionary *frameProperties = [NSDictionary
                                     dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.1], (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 * gifproperty = [NSDictionary dictionaryWithObject:dict forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    //合成gif
    for (NSString * imagepath in imagePathArray)
    {
        UIImage *image=[UIImage imageWithContentsOfFile:imagepath];
        
        CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
    }

    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifproperty);
    CGImageDestinationFinalize(destination);
    
    CFRelease(destination);
}


demo地址:https://github.com/chjwrr/gif_decomposeAndCompose

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容