IPHONE GIF 播放的方式

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

.h

@interface webViewController : UIViewController {
	IBOutlet UIWebView *WebView;
	IBOutlet UINavigationBar *Nav;
}
@property (nonatomic, retain) IBOutlet UIWebView *WebView;
@end

.m

- (void)viewDidLoad {
    [super viewDidLoad];
	[WebView setOpaque:NO];
	[WebView setBackgroundColor:[UIColor clearColor]];
	Nav.topItem.title=@"Welcome";
	Nav.topItem.leftBarButtonItem=nil;	
	NSString* gifFileName = @"j_0002.gif";
	NSMutableString* htmlStr = [NSMutableString string];
	[htmlStr appendString:@"Hello Honey"];
	[htmlStr appendString:@"<p><img src=\""];
	[htmlStr appendFormat:@"%@",gifFileName];
	[htmlStr appendString:@"\" alt=\"picture\"/>"];
	[WebView loadHTMLString:htmlStr baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
}

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


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

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


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

- (NSMutableArray*)getImgArrayByName:(NSString*)name
{
	//happybaby20
	NSURL *threeUrl =[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"crad" 
																			ofType:@"gif"]];
	NSData* data = [[NSData alloc]initWithContentsOfURL:threeUrl];
	NSMutableArray* array = nil;
//	long word = ::SizeofResource(handle,hrsrc);
//	char* lpBy = (char*)LoadResource(handle,hrsrc);
	DWORD word = data.length;
	BYTE* lpBy = (BYTE*)data.bytes;
	//每张图保存的指针 需要释放
	BYTE* pByte[100] = {0};
	//每张图的大小
	DWORD nu[100] = {0};
	//图片数量
	int num = 0;
	DWORD firstLocation = 0;
	for(DWORD j=0;j<word;j++)
	{
		if(lpBy[j]==0x2c)
		{
			if(lpBy[j-1]==0x00)
			{				
				if(num==0)
				{
                    firstLocation = j;
				}
				
				if (num== 10)
				{
					int sdf=nu[num];
					int sdfadf=0;
				}
				
                PGifImage nowImage = (PGifImage)&lpBy[j+1];
				
				if(nowImage->Flag.a==0)
				{
					DWORD number = 1+sizeof(GifImage);
					while(lpBy[j+number]!=0)
					{
					    number = number+(DWORD)lpBy[j+number]+1;
					}
					number++;
					pByte[num] = new BYTE[number];
					memset(pByte[num],0,number);
					for(DWORD n=0;n<number;n++)
					{
						*(BYTE*)(pByte[num]+n) = lpBy[j+n];
					}
					nu[num] = number;
					j = j+number-1;
					num++;
				}
				else
				{
					//int number = 1+ sizeof(GifImage) + 3*(int)floor(pow(2.0f,nowImage->Flag.d));
					
					DWORD number = 1+sizeof(GifImage)+1+3*(int)floor(pow(2.0f,nowImage->Flag.d));
					while(lpBy[j+number] != 0)
					{
					    number = number+lpBy[j+number];
					}
					pByte[num] = new BYTE[number];
					memset(pByte[num],0,number);
					for(int n=0;n<number;n++)
					{
						*(BYTE*)(pByte[num]+n) = lpBy[j+n];
					}
					nu[num] = number;
					j = j+number-1;
					num++;
				}
				

			}
		}
	}

	NSArray *userPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentDirectory = [userPaths objectAtIndex:0];
	
	//数量上
	if (num>0)
	{
		array = [[NSMutableArray alloc] initWithCapacity:num];
	}
	for (int k=0; k<num; k++)
	{
		for(DWORD n=0;n<nu[k];n++)
		{
			lpBy[firstLocation+n] = *(BYTE*)(pByte[k]+n);
		}
		
		NSData* imgbuf = [[NSData alloc] initWithBytes:lpBy length:nu[k] + firstLocation];

		if (imgbuf)
		{

			UIImage* img = [[UIImage alloc]initWithData:imgbuf];
			
			NSData* imgdd = UIImagePNGRepresentation(img);
			
			[imgdd writeToFile:[NSString stringWithFormat:@"%@/sdf%d.png",documentDirectory,k] atomically:NO];
			
			if (img)
			{
				[array addObject:img];
			}
			
			[img release];
		}
		[imgbuf release];
		
		delete pByte[k];
		pByte[k] = NULL;
	}

	return array;
}


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

#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface GifView : UIView {
	CGImageSourceRef gif;
	NSDictionary *gifProperties;
	size_t index;
	size_t count;
	NSTimer *timer;
}
- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath;
@end

.m

#import "GifView.h"
#import <QuartzCore/QuartzCore.h>

@implementation GifView


- (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] retain];
		gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);
		count =CGImageSourceGetCount(gif);
		timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(play) userInfo:nil repeats:YES];
		[timer fire];
    }
    return self;
}
-(void)play
{
	index ++;
	index = index%count;
	CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
	self.layer.contents = (id)ref;
}
-(void)removeFromSuperview
{
	NSLog(@"removeFromSuperview");
	[timer invalidate];
	timer = nil;
	[super removeFromSuperview];
}
- (void)dealloc {
		NSLog(@"dealloc");
	CFRelease(gif);
	[gifProperties release];
    [super dealloc];
}


@end

DEMO

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




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值