//
//  QJGIFView.m
//  xiaoquguanjia
//
//  Created by WuXian on 16/1/18.
//  Copyright © 2016年 buybal. All rights reserved.
//

#import "QJGIFView.h"

#import <QuartzCore/QuartzCore.h>

@implementation QJGIFView

- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
                                                    forKey:(NSString *)kCGImagePropertyGIFDictionary];
        gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);
        count =CGImageSourceGetCount(gif);
        timer = [NSTimer scheduledTimerWithTimeInterval:0.12 target:self selector:@selector(play) userInfo:nil repeats:YES];
        [timer fire];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame data:(NSData *)_data{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
                                                    forKey:(NSString *)kCGImagePropertyGIFDictionary];
        //        gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);
        gif = CGImageSourceCreateWithData((CFDataRef)_data, (CFDictionaryRef)gifProperties);
        count =CGImageSourceGetCount(gif);
        timer = [NSTimer scheduledTimerWithTimeInterval:0.12 target:self selector:@selector(play) userInfo:nil repeats:YES];
        [timer fire];
    }
    return self;
}

- (void)startWithData:(NSData *)gifData
{
    if (gifData == nil) {
        gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"jumafu_loading.gif" ofType:nil]];
    }
    
    gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
                                                forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    gif = CGImageSourceCreateWithData((CFDataRef)gifData, (CFDictionaryRef)gifProperties);
    count =CGImageSourceGetCount(gif);
    timer = [NSTimer scheduledTimerWithTimeInterval:0.12 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 = (__bridge id)ref;
    CFRelease(ref);
}
-(void)removeFromSuperview
{
    NSLog(@"removeFromSuperview");
    [timer invalidate];
    timer = nil;
    [super removeFromSuperview];
}
- (void)dealloc {
    NSLog(@"dealloc");
    CFRelease(gif);
}
- (void)stopGif
{
    [timer invalidate];
    timer = nil;
}

- (NSData *)loadDataForType:(NSInteger)type {
    NSData *data = nil;
    if (type == 0) {
        //网络
        data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://s14.sinaimg.cn/mw690/005APVsyzy6MFOsVFfv5d&690"]];
    }else {
        //本地
        data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"run" ofType:@"gif"]];
    }
    return data;
}




@end
