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
原创粉丝点击