Image I/O
2016-03-12 19:23
393 查看
Image I/O 是 iOS SDK 提供的一个图片编解码器,支持 JPEG, JPEG2000, RAW, TIFF, BMP, GIF, and PNG ,支持 metadata 读取,提供颜色管理。
CGImageSource :数据读取的接口,支持 URL, CFData, or data consumer 。提供读取 CGImage, Thumbnail CGImage, image properties 等等。
CGImageDestination:数据写入的接口,支持 URL, CFData, or data consumer 。可写入单个或多个 CGImage, 包括 Thumbnail CGImage, image properties等。
.h文件
.m文件
CGImageSource :数据读取的接口,支持 URL, CFData, or data consumer 。提供读取 CGImage, Thumbnail CGImage, image properties 等等。
CGImageDestination:数据写入的接口,支持 URL, CFData, or data consumer 。可写入单个或多个 CGImage, 包括 Thumbnail CGImage, image properties等。
#import "ViewController.h" #import "GifView.h" @interface ViewController () @property (nonatomic, strong) GifView *gifView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //将资源文件打包 NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Source.bundle" ofType:nil]; NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; NSString *filePath = [bundle pathForResource:@"info.plist" ofType:nil]; NSArray *dataArr = [NSArray arrayWithContentsOfFile:filePath]; NSDictionary *dict = dataArr[0]; NSString *gifPath = [bundle pathForResource:dict[@"png"] ofType:nil]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:gifPath]]; imageView.userInteractionEnabled = YES; imageView.center = CGPointMake(150, 150); [self.view addSubview:imageView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)]; [imageView addGestureRecognizer:tap]; } - (void)tapGesture:(UITapGestureRecognizer *)gesture { NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Source.bundle" ofType:nil]; NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; NSString *filePath = [bundle pathForResource:@"info.plist" ofType:nil]; NSArray *dataArr = [NSArray arrayWithContentsOfFile:filePath]; NSDictionary *dict = dataArr[0]; NSString *gifPath = [bundle pathForResource:dict[@"gif"] ofType:nil]; NSURL *url = [NSURL fileURLWithPath:gifPath]; GifView *gifView = [[GifView alloc] initWithCenter:CGPointMake(150, 150) fileURL:url]; self.gifView = gifView; [self.view addSubview:gifView]; [self.gifView startGif]; } @end
.h文件
#import <UIKit/UIKit.h> @interface GifView : UIView /* * @brief desingated initializer */ - (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL; /* * @brief start Gif Animation */ - (void)startGif; /* * @brief stop Gif Animation */ - (void)stopGif; /* * @brief get frames image(CGImageRef) in Gif */ + (NSArray*)framesInGif:(NSURL*)fileURL; @end
.m文件
#import "GifView.h" #import <ImageIO/ImageIO.h> #import <QuartzCore/CoreAnimation.h> /* * @brief resolving gif information */ void getFrameInfo(CFURLRef url, NSMutableArray *frames, NSMutableArray *delayTimes, CGFloat *totalTime,CGFloat *gifWidth, CGFloat *gifHeight) { // 读取图片数据 CGImageSourceRef gifSource = CGImageSourceCreateWithURL(url, NULL); // get frame count 获取图片数量 size_t frameCount = CGImageSourceGetCount(gifSource); for (size_t i = 0; i < frameCount; ++i) { // get each frame 获取图片大小 CGImageRef frame = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); [frames addObject:(__bridge id)frame]; CGImageRelease(frame); // get gif info with each frame 获取图片信息 NSDictionary *dict = (NSDictionary*)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(gifSource, i, NULL)); NSLog(@"kCGImagePropertyGIFDictionary %@", [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary]); // get gif size if (gifWidth != NULL && gifHeight != NULL) { *gifWidth = [[dict valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue]; *gifHeight = [[dict valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue]; } // kCGImagePropertyGIFDictionary中kCGImagePropertyGIFDelayTime,kCGImagePropertyGIFUnclampedDelayTime值是一样的 NSDictionary *gifDict = [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary]; [delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]]; if (totalTime) { *totalTime = *totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue]; } } } @interface GifView () { NSMutableArray *_frames; NSMutableArray *_frameDelayTimes; CGFloat _totalTime; // seconds CGFloat _width; CGFloat _height; } @end @implementation GifView - (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL; { self = [super initWithFrame:CGRectZero]; if (self) { self.backgroundColor = [UIColor clearColor]; _frames = [[NSMutableArray alloc] init]; _frameDelayTimes = [[NSMutableArray alloc] init]; _width = 0; _height = 0; if (fileURL) { getFrameInfo((__bridge CFURLRef)fileURL, _frames, _frameDelayTimes, &_totalTime, &_width, &_height); } self.frame = CGRectMake(0, 0, _width, _height); self.center = center; } return self; } + (NSArray*)framesInGif:(NSURL *)fileURL { NSMutableArray *frames = [NSMutableArray arrayWithCapacity:3]; NSMutableArray *delays = [NSMutableArray arrayWithCapacity:3]; getFrameInfo((__bridge CFURLRef)fileURL, frames, delays, NULL, NULL, NULL); return frames; } - (void)startGif { CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"]; NSMutableArray *times = [NSMutableArray arrayWithCapacity:3]; CGFloat currentTime = 0; int count = (int)_frameDelayTimes.count; for (int i = 0; i < count; ++i) { [times addObject:[NSNumber numberWithFloat:(currentTime / _totalTime)]]; currentTime += [[_frameDelayTimes objectAtIndex:i] floatValue]; } [animation setKeyTimes:times]; NSMutableArray *images = [NSMutableArray arrayWithCapacity:3]; for (int i = 0; i < count; ++i) { [images addObject:[_frames objectAtIndex:i]]; } [animation setValues:images]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]; animation.duration = _totalTime; animation.delegate = self; animation.repeatCount = 1; [self.layer addAnimation:animation forKey:@"gifAnimation"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_totalTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self stopGif]; }); } - (void)stopGif { [self.layer removeAllAnimations]; [self removeFromSuperview]; } // remove contents when animation end - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { self.layer.contents = nil; }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用中UISearchDisplayController搜索效果的用法
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- 浅析iOS应用开发中线程间的通信与线程安全问题
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- iOS推送的那些事
- IOS 改变键盘颜色代码