美文网首页
GIF图片的播放和生成

GIF图片的播放和生成

作者: zhouios | 来源:发表于2017-03-31 14:13 被阅读0次

首先需要导入头文件

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

GIF图片的播放

    NSString *gifpath = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:gifpath];
    
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    //将gif分解成一帧帧
    size_t count = CGImageSourceGetCount(source);
    //    NSLog(@"===%zu",count);
    NSMutableArray *tempArray = [NSMutableArray array];
    for (size_t i = 0; i < count; i++) {
        CGImageRef imageref = CGImageSourceCreateImageAtIndex(source, i, NULL);
        UIImage *image = [UIImage imageWithCGImage:imageref scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        [tempArray addObject:image];
        //释放内存
        CGImageRelease(imageref);
    }
    CFRelease(source);
    
    //将图片保存
//        for (int i = 0; i < tempArray.count; i++) {
//            NSData *data = UIImagePNGRepresentation(tempArray[i]);
//    
//            NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//            NSString *gifpath = path[0];
//            NSString *pathNum = [gifpath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
//    
//            [data writeToFile:pathNum atomically:NO];
//        }
    UIImageView *imageview = [[UIImageView alloc] init];
    imageview.bounds = CGRectMake(0, 0, 180, 180);
    imageview.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
    [self.view addSubview:imageview];
    
    [imageview setAnimationImages:tempArray];
    [imageview setAnimationRepeatCount:3];
    [imageview setAnimationDuration:2];
    
    [imageview startAnimating];

GIF图片的生成

    NSMutableArray *images = [NSMutableArray array];
    for (int i = 0; i < 24; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"Documents%d",i]];//加载图片
        [images addObject:image];
    }
    
    //创建gif文件
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentstr = [document objectAtIndex:0];
    NSFileManager *filemanager = [NSFileManager defaultManager];
    NSString *textDic = [documentstr stringByAppendingString:@"/gif"];
    [filemanager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [textDic stringByAppendingString:@"test1.gif"];
    NSLog(@"===%@",path);
    //配置gif属性
    CGImageDestinationRef destion;
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, images.count, NULL);
    
    //    NSDictionary *frameDic = [NSDictionary dictionaryWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3],(NSString *)kCGImagePropertyGIFDelayTime, nil] forKeys:(NSString *)kCGImagePropertyGIFDelayTime];
    
    NSDictionary *frameDic = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.0f] forKey:(NSString *)kCGImagePropertyGIFDelayTime] forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    
    
    NSMutableDictionary *gifParmdict = [NSMutableDictionary dictionaryWithCapacity:2];
    [gifParmdict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
    [gifParmdict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
    [gifParmdict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
    [gifParmdict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
    NSDictionary *gifProperty = [NSDictionary dictionaryWithObject:gifParmdict forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    //单帧添加到GIF
    for (UIImage *dimage in images) {
        CGImageDestinationAddImage(destion, dimage.CGImage, (__bridge CFDictionaryRef)frameDic);
    }
    
    CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
    CGImageDestinationFinalize(destion);
    CFRelease(destion);

相关文章

  • GIF图片的播放和生成

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

  • iOS-播放gif动画文件(OC方法)

    iOS-.gif动画文件的播放 前言 播放gif动画的方法有多种: 将gif图片分解成多张图片使用UIImageV...

  • iOS 播放 gif 动画

    使用UIWebView播放 将GIF图片分解成多张PNG图片,使用UIImageView播放

  • gif 图片播放

    使用 UIWebView 播放 使用 UIImageView + SDWebImage 自定义 MBProgres...

  • 播放Gif图片

    // 1.加载Gif图片, 并且转成Data类型 guard let path = Bundle.main.pat...

  • 【GIF图片的播放】

    具体实现步骤 1.加载gif图片,并且转成Data类型2.从data中读取数据:将data转成CGImageSou...

  • iOS swift 手动实现播放GIF图片

    GIF图片本身就是一帧帧的图片,想要手动实现GIF图片的播放,需要将GIF图片里边的每一帧图片获取到,然后利用UI...

  • iOS-GIF图播放

    封装播放GIF图片的Imageview分类GIF图片来源为本地或网络导入系统库: import ImageIO 加...

  • iOS中展示gif图片(swift)

    需要导入框架: 实现步骤: 获取gif图片 获取组成gif图片的总图片数量值 通过遍历,获取单张图片,及其播放时间...

  • ImageMagick之制作gif图片

    ImageMagick之制作gif图片 gif动画由一系列图片按照一定的时间间隔来播放的,每张单独的图片作为gif...

网友评论

      本文标题:GIF图片的播放和生成

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