美文网首页
ios 生成gif图片

ios 生成gif图片

作者: 念念不忘一个丫头的容 | 来源:发表于2017-04-18 14:16 被阅读0次

需要引入库

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/UTCoreTypes.h>

#import <AssetsLibrary/ALAsset.h>

#import <AssetsLibrary/ALAssetsLibrary.h>

#import <AssetsLibrary/ALAssetsGroup.h>

#import <AssetsLibrary/ALAssetRepresentation.h>
/ 生成gif图片
-(void)shengChengGifImgViewAction{
    //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返回图像目标 kUTTypeGIF  CFStringRef

    destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, self.picArr.count, NULL);
    
    //设置gif的信息,播放间隔时间,基本数据,和delay时间
    NSDictionary *frameProperties = [NSDictionary
                                     dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.001f], (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 self.picArr)
    {
        CGImageDestinationAddImage(destination, dImg.CGImage, (__bridge CFDictionaryRef)frameProperties);
    }
    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);
    
    
     NSLog(@"animated GIF file created at %@", path);
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    // 保存到本地相册
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageDataToSavedPhotosAlbum:data metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        
        NSLog(@"Success at %@", [assetURL path] );
    }] ;
}

相关文章

  • Mac中的GIF

    为IOS模拟器生成GIF图片 安装GIF神器:GIF Brewery 3 安装成功后打开GIF Brewery 3...

  • ios 生成gif图片

    需要引入库

  • GIF图片的播放和生成

    首先需要导入头文件 GIF图片的播放 GIF图片的生成

  • iOS的GIF动画效果实现

    GIF在iOS中的使用场景 GIF在iOS中的使用场景有以下三个方面。 (1)GIF图片分解为单帧图片。 (2)一...

  • iOS Gif图片加载

    Gif图片如越来越受欢迎,移动端对它的支持也是有些知识点的,主要是加载图片性能优化 Gif图片的生成 Gif图片是...

  • 【python实战】生成个性二维码

    一、需求 根据现有的网址、图片或gif,生成二维码,其中,包括根据多张图片生成gif动图,简单易学,上手就会! 二...

  • Swift (四) gif图片播放

    @[TOC](IOS gif图片播放 swift) 1. GIF在iOS平台上的几种加载方式 使用Dispatch...

  • GIF

    imageIO介绍image解压缩图片帧延迟设置bridge gif生成 gif解析

  • JAVA实现多张PNG生成GIF图片

    依赖下载 animated-gif-lib-1.4.jar 多张PNG图片生成GIF图片算法(可抽帧压缩) 把PN...

  • iOS 模拟器录制屏幕生成Gif

    Mac iOS 模拟器录制屏幕生成Gif 有的时候我们需要将iOS app 的运行效果录制下来并保存成gif动画,...

网友评论

      本文标题:ios 生成gif图片

      本文链接:https://www.haomeiwen.com/subject/epwfzttx.html