之前的项目代码里用到了关于gif的显示,现在特别记录一下
效果:
代码
GifView.h
#import#import @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
- (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]; }
#import#import "GifView.h" @interface GifCell : UITableViewCell { GifView *showGifView; } - (void)loadData:(NSData *)data;
#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 }
#import "GifCell.h" #import "ViewController.h" #import "GifView.h" @interface ViewController ()@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