Image I/O
来源:互联网 发布:配置php开发环境 编辑:程序博客网 时间:2024/06/06 05:28
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等。
#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;}
0 0
- Image I/O使用
- Image I/O
- Image I/O编程指南
- Image I/O 编程指引
- Image I/O编程指南
- Image I/O编程指南
- Image I/O编程指南
- Image I/O编程指南
- IOS Image I/O编程指南
- I/O
- I/O
- I/O
- I/O
- I/O
- I/O
- I/O
- I/O
- I/O
- 四大机器学习降维算法:PCA、LDA、LLE、Laplacian Eigenmaps
- 单选对话框
- 抓包工具:Charles-断点
- android 性能专项
- 证明正方形和圆有交点
- Image I/O
- 两个链表的第一个公共结点
- 单片机驱动继电器
- spring @RequestBody The request sent by the client was syntactically incorrect.
- hdu3829
- Java线程:概念与原理
- C++字符串学习(三)
- 函数指针的用途
- 聊聊R的优势