ÏÖÔÚ¿ª·¢³ÌÐò¶¼ÒªÊ¹ÓÃͼƬչʾºÜ¶à£¬µ«ÊǺܶàʱºò£¬²»Í¬µÄչʾ»á¸ø²úÆ·´øÀ´²»Í¬µÄ¸ºµ££¬½ÓÏÂÀ´ÖйúEÃ˼¼ÊõƵµÀС±à´øÄãÒ»ÆðÀ´¿´¿´iOSÖ®GifͼƬչʾµÄ¼¸ÖÖ·½Ê½£¬¾ßÌåÄÚÈÝÈçÏ¡£
ÔÉú·½·¨:
1.UIWebView
ÌØµã:¼ÓÔØËÙ¶ÈÂÔ³¤,ÐÔÄܸüÓÅ£¬²¥·ÅµÄgif¶¯Ì¬Í¼¸ü¼ÓÁ÷³©¡£
//¶¯Ì¬Õ¹Ê¾GIFͼƬ-WebView
-(void)showGifImageWithWebView{
//¶ÁÈ¡gifͼƬÊý¾Ý
NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"earthGif" ofType:@"gif"]];
//UIWebViewÉú³É
UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(112, 302, 132, 102)];
//Óû§²»¿É½»»¥
imageWebView.userInteractionEnabled = NO;
//¼ÓÔØgifÊý¾Ý
[imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
//ÊÓͼÌí¼Ó´Ëgif¿Ø¼þ
[self.view addSubview:imageWebView];
}
2.UIImagView
¼ÓÔØµÄ·½Ê½¸ü¼Ó¿ìËÙ,ÐÔÄܲ»ÈçUIWebView,Óŵã:Ò×ÓÚÀ©Õ¹
1)
Ôö¼ÓÒ»¸öUIImageViewµÄÀà±ð(category),Ôö¼ÓÁ½¸ö·½·¨
UIImage+Tool
.h
#import <UIKit/UIKit.h> @interface UIImageView (Tool) /** ½âÎögifÎļþÊý¾ÝµÄ·½·¨ blockÖлὫ½âÎöµÄÊý¾Ý´«µÝ³öÀ´ */ -(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray,NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths, NSArray<NSNumber *>* heights))dataBlock; /** ΪUIImageViewÌí¼ÓÒ»¸öÉèÖÃgifͼÄÚÈݵķ½·¨£º */ -(void)yh_setImage:(NSURL *)imageUrl; @end
.m
//
// UIImageView+Tool.m
// OneHelper
//
// Created by qiuxuewei on 16/3/2.
// Copyright 2016Äê Çñѧΰ. All rights reserved.
//
#import "UIImageView+Tool.h"
//ÒªÒýÈëImageIO¿â
#import <ImageIO/ImageIO.h>
@implementation UIImageView (Tool)
//½âÎögifÎļþÊý¾ÝµÄ·½·¨ blockÖлὫ½âÎöµÄÊý¾Ý´«µÝ³öÀ´
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray, NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths,NSArray<NSNumber *>* heights))dataBlock{
//ͨ¹ýÎļþµÄurlÀ´½«gifÎļþ¶ÁȡΪͼƬÊý¾ÝÒýÓÃ
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
//»ñÈ¡gifÎļþÖÐͼƬµÄ¸öÊý
size_t count = CGImageSourceGetCount(source);
//¶¨ÒåÒ»¸ö±äÁ¿¼Ç¼gif²¥·ÅÒ»ÂÖµÄʱ¼ä
float allTime=0;
//´æ·ÅËùÓÐͼƬ
NSMutableArray * imageArray = [[NSMutableArray alloc]init];
//´æ·Åÿһ֡²¥·ÅµÄʱ¼ä
NSMutableArray * timeArray = [[NSMutableArray alloc]init];
//´æ·ÅÿÕÅͼƬµÄ¿í¶È £¨Ò»°ãÔÚÒ»¸ögifÎļþÖУ¬ËùÓÐͼƬ³ß´ç¶¼»áÒ»Ñù£©
NSMutableArray * widthArray = [[NSMutableArray alloc]init];
//´æ·ÅÿÕÅͼƬµÄ¸ß¶È
NSMutableArray * heightArray = [[NSMutableArray alloc]init];
//±éÀú
for (size_t i=0; i<count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
[imageArray addObject:(__bridge UIImage *)(image)];
CGImageRelease(image);
//»ñȡͼƬÐÅÏ¢
NSDictionary * info = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, i, NULL);
CGFloat width = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelWidth] floatValue];
CGFloat height = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelHeight] floatValue];
[widthArray addObject:[NSNumber numberWithFloat:width]];
[heightArray addObject:[NSNumber numberWithFloat:height]];
NSDictionary * timeDic = [info objectForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary];
CGFloat time = [[timeDic objectForKey:(__bridge NSString *)kCGImagePropertyGIFDelayTime]floatValue];
allTime+=time;
[timeArray addObject:[NSNumber numberWithFloat:time]];
}
dataBlock(imageArray,timeArray,allTime,widthArray,heightArray);
}
//ΪUIImageViewÌí¼ÓÒ»¸öÉèÖÃgifͼÄÚÈݵķ½·¨£º
-(void)yh_setImage:(NSURL *)imageUrl{
__weak id __self = self;
[self getGifImageWithUrk:imageUrl returnData:^(NSArray<UIImage *> *imageArray, NSArray<NSNumber *> *timeArray, CGFloat totalTime, NSArray<NSNumber *> *widths, NSArray<NSNumber *> *heights) {
//Ìí¼ÓÖ¡¶¯»
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
NSMutableArray * times = [[NSMutableArray alloc]init];
float currentTime = 0;
//ÉèÖÃÿһ֡µÄʱ¼äÕ¼±È
for (int i=0; i<imageArray.count; i++) {
[times addObject:[NSNumber numberWithFloat:currentTime/totalTime]];
currentTime+=[timeArray[i] floatValue];
}
[animation setKeyTimes:times];
[animation setValues:imageArray];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
//ÉèÖÃÑ»·
animation.repeatCount= MAXFLOAT;
//ÉèÖò¥·Å×Üʱ³¤
animation.duration = totalTime;
//Layer²ãÌí¼Ó
[[(UIImageView *)__self layer]addAnimation:animation forKey:@"gifAnimation"];
}];
}
@end
ÔÚ¼ÓÔØgifµÄµØ·½Ê¹ÓÃ
µ¼Èë UIImageView+Tool
-(void)showGifImageWithImageView{
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(112, 342, 132, 102)];
NSURL * url = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"earthGif.gif" ofType:nil]];
[imageView yh_setImage:url];
[self.view addSubview:imageView];
}
µÚÈý·½:
1.YLGIFImage
githubÁ´½Ó: https://github.com/liyong03/YLGIFImage
#import "YLGIFImage.h"
#import "YLImageView.h"
-(void)showGifImageWithYLImageView{
YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(112, 342, 132, 102)];
CGFloat centerX = self.view.center.x;
[imageView setCenter:CGPointMake(centerX, 402)];
[self.view addSubview:imageView];
imageView.image = [YLGIFImage imageNamed:@"earthGif.gif"];
}
2.FLAnimatedImage
githubÁ´½Ó:https://github.com/Flipboard/FLAnimatedImage
-(void)showGifImageWithFLAnimatedImage{
//GIF ת NSData
//Gif ·¾¶
NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"earthGif" ofType:@"gif"];
//ת³ÉNSData
NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
//³õʼ»¯FLAnimatedImage¶ÔÏó
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:dataOfGif];
//³õʼ»¯FLAnimatedImageView¶ÔÏó
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
//ÉèÖÃGIFͼƬ
imageView.animatedImage = image;
imageView.frame = CGRectMake(112, 342, 132, 102);
[self.view addSubview:imageView];
}
ÒÔÉϾÍÊÇiOSÖ®GifͼƬչʾµÄ¼¸ÖÖ·½Ê½µÄÈ«²¿ÄÚÈÝ£¬Ï£Íû¶Ô´ó¼ÒµÄѧϰÓÐËù°ïÖú£¬´ó¼ÒÒªÊÇϲ»¶ÖйúEÃ˼¼ÊõƵµÀС±à·¢²¼µÄÄÚÈÝ£¬¿ÉÒÔÊÕ²ØÎÒÃǵÄÍøÕ¾£¬ËæÊ±À´¿´ÎÄÕÂŶ£¡





