iOS 第三方控件GIFView的学习记录
来源:互联网 发布:户外驴友用哪个软件 编辑:程序博客网 时间:2024/06/01 09:26
之前的项目代码里用到了关于gif的显示,现在特别记录一下
效果:
代码
GifView.h
#import <UIKit/UIKit.h>#import <ImageIO/ImageIO.h>@interface GifView : UIView {CGImageSourceRef gif; // 保存gif动画NSDictionary *gifProperties; // 保存gif动画属性size_t index; // gif动画播放开始的帧序号size_t count; // gif动画的总帧数NSTimer *timer; // 播放gif动画所使用的timer}- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath;- (id)initWithFrame:(CGRect)frame data:(NSData *)_data;- (void)loadData:(NSData *)_data;- (id)initWithFrame:(CGRect)frame;@end
GifView.m
- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { } return self;}-(void)loadData:(NSData *)_data{ //kCGImagePropertyGIFLoopCount loopCount(播放次数):有些gif播放到一定次数就停止了,如果为0就代表gif一直循环播放。 gifProperties = [[NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount] forKey:(NSString *)kCGImagePropertyGIFDictionary] retain]; gif = CGImageSourceCreateWithData((CFDataRef)_data, (CFDictionaryRef)gifProperties); count =CGImageSourceGetCount(gif); //获取图像的属性信息 NSDictionary* frame1Properties = ( NSDictionary*)CGImageSourceCopyPropertiesAtIndex(gif, 0, NULL); //kCGImagePropertyGIFDelayTime 每一帧播放的时间,也就是说这帧显示到delayTime就转到下一帧。 float duration = [[[frame1Properties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue]; //解决重复调用timer的问题 if (timer) { [timer invalidate]; timer = nil; } timer = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(play) userInfo:nil repeats:YES]; [timer fire];}-(void)play{ index ++; index = index%count; //获取图像CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);self.layer.contents = (id)ref; CFRelease(ref);}-(void)removeFromSuperview{NSLog(@"removeFromSuperview");[timer invalidate];timer = nil;[super removeFromSuperview];}- (void)dealloc { NSLog(@"dealloc");CFRelease(gif);[gifProperties release]; [super dealloc];}
GifCell.h
#import <UIKit/UIKit.h>#import "GifView.h"@interface GifCell : UITableViewCell{ GifView *showGifView; }- (void)loadData:(NSData *)data;
GifCell.m
#import "GifCell.h"@implementation GifCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self performSelector:@selector(makeCell)]; } return self;}- (void)makeCell{ self.selectionStyle = UITableViewCellSelectionStyleNone; self.contentView.backgroundColor = RGBCOLOR(215, 219, 220); UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 55, 50)]; headView.backgroundColor = RGBCOLOR(213, 214, 216); [self.contentView addSubview:headView]; UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(55, 0, 1, 50)]; lineView.backgroundColor = RGBCOLOR(188, 190, 189); [self.contentView addSubview:lineView]; UIImageView *imageRound = [[UIImageView alloc]initWithFrame:CGRectMake(14, 12, 28, 28)]; imageRound.image = [UIImage imageNamed:@"radiobutton_off_background"]; [headView addSubview:imageRound]; showGifView =[[GifView alloc] initWithFrame:CGRectMake(65, 5, 300, 40) ]; showGifView.contentMode = UIViewContentModeScaleAspectFit; [self.contentView addSubview:showGifView]; }- (void)loadData:(NSData *)data{ [showGifView loadData:data];}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ [super setSelected:selected animated:animated]; // Configure the view for the selected state}
ViewController.m
#import "GifCell.h"#import "ViewController.h"#import "GifView.h"@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>@property (nonatomic, strong)NSMutableArray *dataArr;@property (nonatomic, strong)UITableView *myTableView;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; self.dataArr = [[NSMutableArray alloc]initWithCapacity:0]; for (int i = 0 ; i<10 ; i++) { NSString *dataPath = [[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%d",i+1] ofType:@"gif"]; NSData *data = [[NSData alloc]initWithContentsOfFile:dataPath]; [self.dataArr addObject:data]; } self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 320, 300) style:UITableViewStylePlain]; self.myTableView.delegate = self; self.myTableView.dataSource = self; [self.view addSubview:self.myTableView]; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArr.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ GifCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"]; if (!cell) { cell = [[GifCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"]; } [cell loadData:[self.dataArr objectAtIndex:indexPath.row]]; return cell;}
源码:GIFViewDemo
0 0
- iOS 第三方控件GIFView的学习记录
- Android中第三方控件gifView的基本使用
- iOS - 第三方库 SAVideoRangeSlider 的代码学习记录
- iOS - 第三方库 SAVideoRangeSlider 的代码学习记录
- 第三方控件记录
- ios各种第三方控件
- iOS第三方库的填坑记录
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- 一些iOS常用的第三方库和控件
- IOS OC中使用Swift的Charts第三方控件
- git学习笔记
- poj2486解题报告
- HDU 1032(水题)
- USB Phy/ULPI (2-读写USB Phy寄存器)
- 随便写了个堆排序程序
- iOS 第三方控件GIFView的学习记录
- 学习grep命令时候遇到的问题
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
- HDU 1039(水题)
- Cocos2d-x3.0(一)Cocos2d-x3.0简介以及创建新的Cocos2d-x跨平台工程
- 【北京圣思园学习笔记】第04讲:运算符(Operator)
- Android四大组件(摘要)
- Nginx开启Gzip压缩大幅提高页面加载速度及相关测试
- 三种注释