GifView.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //
  2. // GifView.m
  3. // GIFViewer
  4. //
  5. // Created by xToucher04 on 11-11-9.
  6. // Copyright 2011 Toucher. All rights reserved.
  7. //
  8. #import "GifView.h"
  9. #import <QuartzCore/QuartzCore.h>
  10. @implementation GifView
  11. - (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{
  12. self = [super initWithFrame:frame];
  13. if (self) {
  14. gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
  15. forKey:(NSString *)kCGImagePropertyGIFDictionary] ;
  16. gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);
  17. count =CGImageSourceGetCount(gif);
  18. timer = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(play) userInfo:nil repeats:YES];
  19. [timer fire];
  20. }
  21. return self;
  22. }
  23. - (id)initWithFrame:(CGRect)frame data:(NSData *)_data{
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
  27. forKey:(NSString *)kCGImagePropertyGIFDictionary] ;
  28. // gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);
  29. gif = CGImageSourceCreateWithData((CFDataRef)_data, (CFDictionaryRef)gifProperties);
  30. count =CGImageSourceGetCount(gif);
  31. timer = [NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(play) userInfo:nil repeats:YES];
  32. [timer fire];
  33. }
  34. return self;
  35. }
  36. -(void)play
  37. {
  38. index ++;
  39. index = index%count;
  40. CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
  41. self.layer.contents = (__bridge id)ref;
  42. CFRelease(ref);
  43. }
  44. -(void)removeFromSuperview
  45. {
  46. NSLog(@"GifView removeFromSuperview, is MainThread %d", [NSThread isMainThread]);
  47. [timer invalidate];
  48. timer = nil;
  49. [super removeFromSuperview];
  50. }
  51. - (void)dealloc {
  52. NSLog(@"GifView dealloc..., is MainThraed %d", [NSThread isMainThread]);
  53. CFRelease(gif);
  54. NSLog(@"GifView dealloc finish");
  55. }
  56. @end