IPHONE GIF 播放的方式

转 http://blog.csdn.net/zltianhen/article/details/6841466

1.UIWebView 播放(无法控制周期)

.h

 

[cpp]  view plain copy
  1. @interface webViewController : UIViewController {  
  2.     IBOutlet UIWebView *WebView;  
  3.     IBOutlet UINavigationBar *Nav;  
  4. }  
  5. @property (nonatomic, retain) IBOutlet UIWebView *WebView;  
  6. @end  

.m

 

 

[cpp]  view plain copy
  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     [WebView setOpaque:NO];  
  4.     [WebView setBackgroundColor:[UIColor clearColor]];  
  5.     Nav.topItem.title=@"Welcome";  
  6.     Nav.topItem.leftBarButtonItem=nil;    
  7.     NSString* gifFileName = @"j_0002.gif";  
  8.     NSMutableString* htmlStr = [NSMutableString string];  
  9.     [htmlStr appendString:@"Hello Honey"];  
  10.     [htmlStr appendString:@"<p><img src=\""];  
  11.     [htmlStr appendFormat:@"%@",gifFileName];  
  12.     [htmlStr appendString:@"\" alt=\"picture\"/>"];  
  13.     [WebView loadHTMLString:htmlStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];  
  14. }  

2.glgif提供的开源代码实现(目前无法控制周期,需要自己再研究)

 

 

3.使用UIView的animation接口实现(GIF需要整理出每张图片)

动画可以用UIImageView,把动画的每一帧做成图片,转换成UIImage保存在animationImages数组中,调用- (void)startAnimating;就能看到动画

 

4.把GIF的通过GIF的格式转换成每张图片,通过UIView的animation的接口实现(GIF的格式貌似还是有问题,有部分图片的背景有问题)

 

[cpp]  view plain copy
  1. - (NSMutableArray*)getImgArrayByName:(NSString*)name  
  2. {  
  3.     //happybaby20  
  4.     NSURL *threeUrl =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"crad"   
  5.                                                                             ofType:@"gif"]];  
  6.     NSData* data = [[NSData alloc]initWithContentsOfURL:threeUrl];  
  7.     NSMutableArray* array = nil;  
  8. //  long word = ::SizeofResource(handle,hrsrc);  
  9. //  char* lpBy = (char*)LoadResource(handle,hrsrc);  
  10.     DWORD word = data.length;  
  11.     BYTE* lpBy = (BYTE*)data.bytes;  
  12.     //每张图保存的指针 需要释放  
  13.     BYTE* pByte[100] = {0};  
  14.     //每张图的大小  
  15.     DWORD nu[100] = {0};  
  16.     //图片数量  
  17.     int num = 0;  
  18.     DWORD firstLocation = 0;  
  19.     for(DWORD j=0;j<word;j++)  
  20.     {  
  21.         if(lpBy[j]==0x2c)  
  22.         {  
  23.             if(lpBy[j-1]==0x00)  
  24.             {                 
  25.                 if(num==0)  
  26.                 {  
  27.                     firstLocation = j;  
  28.                 }  
  29.                   
  30.                 if (num== 10)  
  31.                 {  
  32.                     int sdf=nu[num];  
  33.                     int sdfadf=0;  
  34.                 }  
  35.                   
  36.                 PGifImage nowImage = (PGifImage)&lpBy[j+1];  
  37.                   
  38.                 if(nowImage->Flag.a==0)  
  39.                 {  
  40.                     DWORD number = 1+sizeof(GifImage);  
  41.                     while(lpBy[j+number]!=0)  
  42.                     {  
  43.                         number = number+(DWORD)lpBy[j+number]+1;  
  44.                     }  
  45.                     number++;  
  46.                     pByte[num] = new BYTE[number];  
  47.                     memset(pByte[num],0,number);  
  48.                     for(DWORD n=0;n<number;n++)  
  49.                     {  
  50.                         *(BYTE*)(pByte[num]+n) = lpBy[j+n];  
  51.                     }  
  52.                     nu[num] = number;  
  53.                     j = j+number-1;  
  54.                     num++;  
  55.                 }  
  56.                 else  
  57.                 {  
  58.                     //int number = 1+ sizeof(GifImage) + 3*(int)floor(pow(2.0f,nowImage->Flag.d));  
  59.                       
  60.                     DWORD number = 1+sizeof(GifImage)+1+3*(int)floor(pow(2.0f,nowImage->Flag.d));  
  61.                     while(lpBy[j+number] != 0)  
  62.                     {  
  63.                         number = number+lpBy[j+number];  
  64.                     }  
  65.                     pByte[num] = new BYTE[number];  
  66.                     memset(pByte[num],0,number);  
  67.                     for(int n=0;n<number;n++)  
  68.                     {  
  69.                         *(BYTE*)(pByte[num]+n) = lpBy[j+n];  
  70.                     }  
  71.                     nu[num] = number;  
  72.                     j = j+number-1;  
  73.                     num++;  
  74.                 }  
  75.                   
  76.   
  77.             }  
  78.         }  
  79.     }  
  80.   
  81.     NSArray *userPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  82.     NSString *documentDirectory = [userPaths objectAtIndex:0];  
  83.       
  84.     //数量上  
  85.     if (num>0)  
  86.     {  
  87.         array = [[NSMutableArray alloc] initWithCapacity:num];  
  88.     }  
  89.     for (int k=0; k<num; k++)  
  90.     {  
  91.         for(DWORD n=0;n<nu[k];n++)  
  92.         {  
  93.             lpBy[firstLocation+n] = *(BYTE*)(pByte[k]+n);  
  94.         }  
  95.           
  96.         NSData* imgbuf = [[NSData alloc] initWithBytes:lpBy length:nu[k] + firstLocation];  
  97.   
  98.         if (imgbuf)  
  99.         {  
  100.   
  101.             UIImage* img = [[UIImage alloc]initWithData:imgbuf];  
  102.               
  103.             NSData* imgdd = UIImagePNGRepresentation(img);  
  104.               
  105.             [imgdd writeToFile:[NSString stringWithFormat:@"%@/sdf%d.png",documentDirectory,k] atomically:NO];  
  106.               
  107.             if (img)  
  108.             {  
  109.                 [array addObject:img];  
  110.             }  
  111.               
  112.             [img release];  
  113.         }  
  114.         [imgbuf release];  
  115.           
  116.         delete pByte[k];  
  117.         pByte[k] = NULL;  
  118.     }  
  119.   
  120.     return array;  
  121. }  


5.QuartzCore播放 比较完美的方案

 

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2. #import <ImageIO/ImageIO.h>  
  3. #import <MobileCoreServices/MobileCoreServices.h>  
  4.   
  5. @interface GifView : UIView {  
  6.     CGImageSourceRef gif;  
  7.     NSDictionary *gifProperties;  
  8.     size_t index;  
  9.     size_t count;  
  10.     NSTimer *timer;  
  11. }  
  12. - (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath;  
  13. @end  

.m

 

 

[cpp]  view plain copy
  1. #import "GifView.h"  
  2. #import <QuartzCore/QuartzCore.h>  
  3.   
  4. @implementation GifView  
  5.   
  6.   
  7. - (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{  
  8.       
  9.     self = [super initWithFrame:frame];  
  10.     if (self) {  
  11.   
  12.         gifProperties = [[NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]  
  13.                                                      forKey:(NSString *)kCGImagePropertyGIFDictionary] retain];  
  14.         gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);  
  15.         count =CGImageSourceGetCount(gif);  
  16.         timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(play) userInfo:nil repeats:YES];  
  17.         [timer fire];  
  18.     }  
  19.     return self;  
  20. }  
  21. -(void)play  
  22. {  
  23.     index ++;  
  24.     index = index%count;  
  25.     CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);  
  26.     self.layer.contents = (id)ref;  
  27. }  
  28. -(void)removeFromSuperview  
  29. {  
  30.     NSLog(@"removeFromSuperview");  
  31.     [timer invalidate];  
  32.     timer = nil;  
  33.     [super removeFromSuperview];  
  34. }  
  35. - (void)dealloc {  
  36.         NSLog(@"dealloc");  
  37.     CFRelease(gif);  
  38.     [gifProperties release];  
  39.     [super dealloc];  
  40. }  
  41.   
  42.   
  43. @end  

DEMO

 

 

[cpp]  view plain copy
  1. GifView *gifview = [[GifView alloc] initWithFrame:CGRectMake(0, 0, 200, 200) filePath:[[NSBundle mainBundle] pathForResource:@"test.gif" ofType:nil]];  
  2.     [self.view addSubview:gifview];  


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值