GIF图的完美拆解合成显示
Posted Ven519
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GIF图的完美拆解合成显示相关的知识,希望对你有一定的参考价值。
最近由于项目需要,需要先把gif图拆解开,然后在每一张图片上添加一些图片和文字,最后再合成gif文件;写了一个工具类可以每一帧画面并遵循每一帧所对应的显示时间进行播放,并且可以用多张图片指定每一帧播放时间来合成gif图。下面是使用方法和工具类:(需要添加framework : ImageIO、QuartzCore、MobileCoreServices)
[cpp] view plain copy
- NSDate *date = [NSDate date];
- //读取本地GIF图中每一帧图像的信息
- NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"demo" withExtension:@"gif"];
- NSDictionary *dic = [GifView getGifInfo:fileUrl];
- NSMutableArray *imageArray = [NSMutableArray array];
- //在gif图的每一帧上面添加一段文字
- for(int index=0;index<[dic[@"images"] count];index++)
- //绘制view 已GIf图中的某一帧为背景并在view上添加文字
- UIView *tempView = [[UIView alloc] initWithFrame:CGRectFromString(dic[@"bounds"])];
- tempView.backgroundColor = [UIColor colorWithPatternImage:dic[@"images"][index]];
- UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 80, 20)];
- tempLabel.text = @"GIF测试";
- tempLabel.textColor = [UIColor redColor];
- tempLabel.backgroundColor = [UIColor clearColor];
- tempLabel.font = [UIFont boldSystemFontOfSize:20];
- [tempView addSubview:tempLabel];
- //将UIView转换为UIImage
- UIGraphicsBeginImageContextWithOptions(tempView.bounds.size, NO, tempView.layer.contentsScale);
- [tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- [imageArray addObject:image];
- UIGraphicsEndImageContext();
- //生成GIF图 -- loopCount 为0表示无限播放
- NSString *path = [GifView exportGifImages:[imageArray copy] delays:dic[@"delays"] loopCount:0];
- //在页面上展示合成之后的GIF图
- GifView *gifView = [[GifView alloc] initWithCenter:self.view.center fileURL:[NSURL fileURLWithPath:path]];
- [self.view addSubview:gifView];
- NSLog(@"合成GIF图用时:%f秒",[[NSDate date] timeIntervalSinceDate:date]);
//GifView.h
[cpp] view plain copy
- #import <UIKit/UIKit.h>
- @interface GifView : UIView
- /*
- * @brief desingated initializer
- */
- - (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;
- - (void)initFileURL:(NSURL*)fileURL;
- /*
- * @brief start Gif Animation
- */
- - (void)startGif;
- - (void)startGifAnimation;
- /*
- * @brief stop Gif Animation
- */
- - (void)stopGif;
- /*
- * @brief get frames image(CGImageRef) in Gif
- */
- + (NSDictionary *)getGifInfo:(NSURL *)fileURL;
- + (NSString *)exportGifImages:(NSArray *)images delays:(NSArray *)delays loopCount:(NSUInteger)loopCount;
- @end
//GifView.m
[cpp] view plain copy- //
- // GifView.m
- //
- // Created by marujun on 13-11-7.
- // Copyright (c) 2013年 极致. All rights reserved.
- //
- #import "GifView.h"
- #import <ImageIO/ImageIO.h>
- #import <QuartzCore/QuartzCore.h>
- #import <MobileCoreServices/MobileCoreServices.h>
- @interface GifView()
- NSMutableArray *_frames;
- NSMutableArray *_frameDelayTimes;
- CGPoint frameCenter;
- CADisplayLink *displayLink;
- int frameIndex;
- double frameDelay;
- NSUInteger _loopCount;
- NSUInteger _currentLoop;
- CGFloat _totalTime; // seconds
- CGFloat _width;
- CGFloat _height;
- @end
- @implementation GifView
- - (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;
- self = [super initWithFrame:CGRectZero];
- if (self)
- _frames = [[NSMutableArray alloc] init];
- _frameDelayTimes = [[NSMutableArray alloc] init];
- _width = 0;
- _height = 0;
- frameCenter = center;
- [self initFileURL:fileURL];
- 完美解决android显示gif