iOS-.gif動畫文件的播放
前言
播放gif動畫的方法有多種:
- 將gif圖片分解成多張圖片使用UIImageView播放
- webView直接播放.gif文件
- 使用第三方播放
本文主要介紹的是這三種方法播放gif動畫臣咖,并提供一個使用第三種方法制作的播放gif的loading實例。接下就步入正題吧(__)裆站!
為了方便大家使用封裝了一個可以在任意VIEW播放gif動畫的工具類雏搂,歡迎使用渗勘!
VDGifPlayerTool.h
#import <Foundation/Foundation.h>
@interface VDGifPlayerTool : NSObject
- (void)addGifWithName:(NSString *)gifName toView:(UIView *)view;
@end
VDGifPlayerTool.m
#import "VDGifPlayerTool.h"
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface VDGifPlayerTool()
@property (nonatomic, strong)UIView *gifContentView;
@property (nonatomic, assign)CGImageSourceRef gif;
@property (nonatomic, strong)NSDictionary *gifDic;
@property (nonatomic, assign)size_t index;
@property (nonatomic, assign)size_t count;
@property (nonatomic, strong)NSTimer *timer;
@end
@implementation VDGifPlayerTool
- (void)addGifWithName:(NSString *)gifName toView:(UIView *)view{
self.gifContentView = view;
[self createGif:gifName];
}
- (void)createGif:(NSString *)name{
// _gifContentView.layer.borderColor = UIColorFromRGB(No_Choose_Color).CGColor;
// _gifContentView.layer.borderWidth = 1.0;
NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
_gifDic = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:name ofType:@"gif"]];
_gif = CGImageSourceCreateWithData((CFDataRef)gif, (CFDictionaryRef)_gifDic);
_count = CGImageSourceGetCount(_gif);
_timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(startLoading) userInfo:nil repeats:YES];
[_timer fire];
}
-(void)startLoading
{
_index ++;
_index = _index%_count;
CGImageRef ref = CGImageSourceCreateImageAtIndex(_gif, _index, (CFDictionaryRef)_gifDic);
self.gifContentView.layer.contents = (__bridge id)ref;
CFRelease(ref);
}
- (void)dealloc{
if (_gif) {
CFRelease(_gif);
}
}
@end
一殃饿、UIImageView實現(xiàn)gif動畫
用imageView制作gif動畫最經典就是湯姆貓茄唐,感興趣的可以百度或者谷歌一下“iOS湯姆貓源代碼”张惹。在這里只是簡單的介紹imageview的gif動畫實現(xiàn)舀锨,你也可以用計時器(NSTimer).在做這些之前必須要將gif分解成一張張PNG圖片。
UIImageView *gifImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
gifImageView.center = self.view.center;
[self.view addSubview:gifImageView];
NSMutableArray *images = [NSMutableArray array];
for (int i=0; i < 10; i++) {
[images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"image%d",i]]];
}
gifImageView.animationImages = images;
gifImageView.animationDuration = 5.0;
gifImageView.animationRepeatCount = NSUIntegerMax;
[gifImageView startAnimating];
二宛逗、UIWebView實現(xiàn).gif動畫文件的播放
webView可以加載很多文件是個很強大的控件坎匿,實現(xiàn)gif播放簡單直接不過只能循環(huán)播放。
CGSize size = [UIImage imageNamed:@"name.gif"].size;
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
webView.center = self.view.center;
webView.userInteractionEnabled = NO;
webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:webView];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"name" ofType:@"gif"]];
[webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
三雷激、第三方實現(xiàn)播放gif動畫
首先需要導入兩個庫:ImageIO.framework和MobileCoreServices.framework;具體實現(xiàn)是采用ImageIO庫對.gif文件進行解析獲取相關資源最后進行動畫的播放碑诉,下面是自己寫的播放gif動畫的loading實例(比較簡陋大家將就看看,知道怎么實現(xiàn)就行了_)侥锦。
GifLoadingView.h
#import <UIKit/UIKit.h>
@interface GifLoadingView : UIView
+(void)startLoading;
+(void)endLoading;
@end
GifLoadingView.m
#define GIF_WIDTH 80*1.2
#import "GifLoadingView.h"
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface GifLoadingView()
@property (nonatomic, strong)NSMutableArray<UIImage *> *images;
@property (nonatomic, strong)GifLoadingView *loading;
@property (nonatomic, strong)UIView *gifContentView;
@property (nonatomic, assign)CGImageSourceRef gif;
@property (nonatomic, strong)NSDictionary *gifDic;
@property (nonatomic, assign)size_t index;
@property (nonatomic, assign)size_t count;
@property (nonatomic, strong)NSTimer *timer;
@end
@implementation GifLoadingView
- (instancetype)init{
self = [self initWithFrame:CGRectMake(0, 0, GIF_WIDTH, GIF_WIDTH)];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.cornerRadius = GIF_WIDTH/2;
self.layer.masksToBounds = YES;
[self createGif];
}
return self;
}
- (void)createGif{
// _gifContentView.layer.borderColor = UIColorFromRGB(No_Choose_Color).CGColor;
// _gifContentView.layer.borderWidth = 1.0;
NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
_gifDic = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary];
NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"]];
_gif = CGImageSourceCreateWithData((CFDataRef)gif, (CFDictionaryRef)_gifDic);
_count = CGImageSourceGetCount(_gif);
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(startLoading) userInfo:nil repeats:YES];
[_timer fire];
}
-(void)startLoading
{
_index ++;
_index = _index%_count;
CGImageRef ref = CGImageSourceCreateImageAtIndex(_gif, _index, (CFDictionaryRef)_gifDic);
self.layer.contents = (__bridge id)ref;
CFRelease(ref);
}
+ (void)startLoading{
GifLoadingView *loading = [[GifLoadingView alloc]init];
UIWindow *keyView = [UIApplication sharedApplication].keyWindow;
loading.center = keyView.center;
[keyView addSubview:loading];
dispatch_main_after(5.0f, ^{
[GifLoadingView endLoading];
});
}
+ (void)endLoading{
for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
if ([view isKindOfClass:[GifLoadingView class]]) {
// [UIView animateWithDuration:1.0 animations:^{
// view.alpha = 0;
// } completion:^(BOOL finished) {
[((GifLoadingView *)view) stopGif];
[view removeFromSuperview];
// }];
}
}
}
- (void)stopGif
{
[_timer invalidate];
_timer = nil;
}
- (void)dealloc{
CFRelease(_gif);
}
static void dispatch_main_after(NSTimeInterval delay, void (^block)(void))
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
block();
});
}
總結
簡要總結一些三個方法的利弊:
UIImageView采用幀動畫將圖片一張張顯示进栽,這個方法可以調節(jié)播放次數(shù)和速度但圖片過多過大內存會很有壓力。另外在保證播放的動畫清晰和時長的情況下.gif文件大小會遠小于多張.png圖片的大小恭垦。
UIWebView實現(xiàn)播放gif特別簡單直接快毛,如果你只想單純的播放一下建議使用此方法。弊端就是只能循環(huán)播放~~~(>_<)~~~番挺,無法控制它的暫停和其他操作唠帝。
使用第三方播放gif動畫集合了第一種方法好處你可以對動畫進行一系列操作,在.gif文件比較大的情況下建議使用玄柏。(個人喜歡使用此方法)襟衰。
最后
我的第二次(寫文章)就這么完了。以上很多觀點為自己的個人看法不喜勿噴粪摘,當然噴了也沒事瀑晒。最后感謝那些能看完小爺這篇文章的小伙伴們,希望能夠幫助到你們徘意。( _ )/~~拜拜苔悦。