我們曉得Gif是由一陣陣畫面構成的,並且每幀畫面播放的經常能夠會不相等,不雅察下面兩個例子,發明他們都沒有對Gif中每幀的顯示經常做處置,如許的成果就是全部Gif中每幀畫面都是以固定的速度向前播放,很明顯這其實不總會相符需求。
因而本身寫一個解析Gif的對象類,處理每幀畫面並遵守每幀所對應的顯示時光停止播放。
法式的思緒以下:
1、起首應用ImageIO庫中的CGImageSource家在Gif文件。
2、經由過程CGImageSource獲得到Gif文件中的總的幀數,和每幀的顯示時光。
3、經由過程CAKeyframeAnimation來完成Gif動畫的播放。
上面直接上我寫的解析和播放Gif的對象類的代碼:
//
// SvGifView.h
// SvGifSample
//
// Created by maple on 3/28/13.
// Copyright (c) 2013 smileEvday. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SvGifView : UIView
/*
* @brief desingated initializer
*/
- (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;
/*
* @brief start Gif Animation
*/
- (void)startGif;
/*
* @brief stop Gif Animation
*/
- (void)stopGif;
/*
* @brief get frames image(CGImageRef) in Gif
*/
+ (NSArray*)framesInGif:(NSURL*)fileURL;
@end
//
// SvGifView.m
// SvGifSample
//
// Created by maple on 3/28/13.
// Copyright (c) 2013 smileEvday. All rights reserved.
//
#import "SvGifView.h"
#import <ImageIO/ImageIO.h>
#import <QuartzCore/CoreAnimation.h>
/*
* @brief resolving gif information
*/
void getFrameInfo(CFURLRef url, NSMutableArray *frames, NSMutableArray *delayTimes, CGFloat *totalTime,CGFloat *gifWidth, CGFloat *gifHeight)
{
CGImageSourceRef gifSource = CGImageSourceCreateWithURL(url, NULL);
// get frame count
size_t frameCount = CGImageSourceGetCount(gifSource);
for (size_t i = 0; i < frameCount; ++i) {
// get each frame
CGImageRef frame = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
[frames addObject:(id)frame];
CGImageRelease(frame);
// get gif info with each frame
NSDictionary *dict = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(gifSource, i, NULL);
NSLog(@"kCGImagePropertyGIFDictionary %@", [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary]);
// get gif size
if (gifWidth != NULL && gifHeight != NULL) {
*gifWidth = [[dict valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue];
*gifHeight = [[dict valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue];
}
// kCGImagePropertyGIFDictionary中kCGImagePropertyGIFDelayTime,kCGImagePropertyGIFUnclampedDelayTime值是一樣的
NSDictionary *gifDict = [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary];
[delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]];
if (totalTime) {
*totalTime = *totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue];
}
}
}
@interface SvGifView() {
NSMutableArray *_frames;
NSMutableArray *_frameDelayTimes;
CGFloat _totalTime; // seconds
CGFloat _width;
CGFloat _height;
}
@end
@implementation SvGifView
- (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;
{
self = [super initWithFrame:CGRectZero];
if (self) {
_frames = [[NSMutableArray alloc] init];
_frameDelayTimes = [[NSMutableArray alloc] init];
_width = 0;
_height = 0;
if (fileURL) {
getFrameInfo((CFURLRef)fileURL, _frames, _frameDelayTimes, &_totalTime, &_width, &_height);
}
self.frame = CGRectMake(0, 0, _width, _height);
self.center = center;
}
return self;
}
+ (NSArray*)framesInGif:(NSURL *)fileURL
{
NSMutableArray *frames = [NSMutableArray arrayWithCapacity:3];
NSMutableArray *delays = [NSMutableArray arrayWithCapacity:3];
getFrameInfo((CFURLRef)fileURL, frames, delays, NULL, NULL, NULL);
return frames;
}
- (void)startGif
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
NSMutableArray *times = [NSMutableArray arrayWithCapacity:3];
CGFloat currentTime = 0;
int count = _frameDelayTimes.count;
for (int i = 0; i < count; ++i) {
[times addObject:[NSNumber numberWithFloat:(currentTime / _totalTime)]];
currentTime += [[_frameDelayTimes objectAtIndex:i] floatValue];
}
[animation setKeyTimes:times];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:3];
for (int i = 0; i < count; ++i) {
[images addObject:[_frames objectAtIndex:i]];
}
[animation setValues:images];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
animation.duration = _totalTime;
animation.delegate = self;
animation.repeatCount = 5;
[self.layer addAnimation:animation forKey:@"gifAnimation"];
}
- (void)stopGif
{
[self.layer removeAllAnimations];
}
// remove contents when animation end
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
self.layer.contents = nil;
}
// Only override drawRect: if you perform custom draWing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// DraWing code
}
@end
代碼很短也比擬輕易,就紛歧一說明了。最開端的誰人C函數重要就是用來解析Gif的,之所以用C函數是由於我要前往多個信息,而Objective-c只能前往一個參數,並且Objective-c和C說話可以很便利的混雜編程。
別的再引見兩種應用UIImageView的辦法:
1. 應用UIWebView播放
// 設定地位和年夜小
CGRect frame = CGRectMake(50,50,0,0);
frame.size = [UIImage imageNamed:@"guzhang.gif"].size;
// 讀取gif圖片數據
NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"guzhang" ofType:@"gif"]];
// view生成
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
webView.userInteractionEnabled = NO;//用戶弗成交互
[webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
[self.view addSubview:webView];
[webView release];
2.將gif圖片分化成多張png圖片,應用UIImageView播放。
代碼以下:
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1"],
[UIImage imageNamed:@"2"],
[UIImage imageNamed:@"3"],
[UIImage imageNamed:@"4"],
[UIImage imageNamed:@"5"],
[UIImage imageNamed:@"6"],
[UIImage imageNamed:@"7"],
[UIImage imageNamed:@"8"],
[UIImage imageNamed:@"9"],
[UIImage imageNamed:@"10"],
[UIImage imageNamed:@"11"],
[UIImage imageNamed:@"12"],
[UIImage imageNamed:@"13"],
[UIImage imageNamed:@"14"],
[UIImage imageNamed:@"15"],
[UIImage imageNamed:@"16"],
[UIImage imageNamed:@"17"],
[UIImage imageNamed:@"18"],
[UIImage imageNamed:@"19"],
[UIImage imageNamed:@"20"],
[UIImage imageNamed:@"21"],
[UIImage imageNamed:@"22"],nil];
gifImageView.animationImages = gifArray; //動繪圖片數組
gifImageView.animationDuration = 5; //履行一次完全動畫所需的時長
gifImageView.animationRepeatCount = 1; //動畫反復次數
[gifImageView startAnimating];
[self.view addSubview:gifImageView];
[gifImageView release];
留意:這個辦法,假如gif動畫每桢間的時光距離分歧,不克不及到達此後果。
【iOS開辟中完成顯示gif圖片的辦法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!