最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS Gif圖片展示N種方式(原生+第三方)

 更新時(shí)間:2016年03月03日 11:59:25   投稿:lijiao  
這篇文章主要介紹了iOS Gif圖片展示N種方式,包括原生、第三方方式展示,感興趣的小伙伴們可以參考一下

本文分享了iOS Gif圖片展示N種方式,供大家參考,具體內(nèi)容如下

原生方法:

1.UIWebView
特點(diǎn):加載速度略長,性能更優(yōu),播放的gif動(dòng)態(tài)圖更加流暢。

//動(dòng)態(tài)展示GIF圖片-WebView
-(void)showGifImageWithWebView{
 //讀取gif圖片數(shù)據(jù)
 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數(shù)據(jù)
 [imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
 //視圖添加此gif控件
 [self.view addSubview:imageWebView];
}

2.UIImagView
加載的方式更加快速,性能不如UIWebView,優(yōu)點(diǎn):易于擴(kuò)展

1)
增加一個(gè)UIImageView的類別(category),增加兩個(gè)方法
UIImage+Tool
.h

#import <UIKit/UIKit.h>

@interface UIImageView (Tool)

/** 解析gif文件數(shù)據(jù)的方法 block中會(huì)將解析的數(shù)據(jù)傳遞出來 */
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray,NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths, NSArray<NSNumber *>* heights))dataBlock;

/** 為UIImageView添加一個(gè)設(shè)置gif圖內(nèi)容的方法: */
-(void)yh_setImage:(NSURL *)imageUrl;

@end

.m

//
// UIImageView+Tool.m
// OneHelper
//
// Created by qiuxuewei on 16/3/2.
// Copyright © 2016年 邱學(xué)偉. All rights reserved.
//

#import "UIImageView+Tool.h"
//要引入ImageIO庫
#import <ImageIO/ImageIO.h>

@implementation UIImageView (Tool)



//解析gif文件數(shù)據(jù)的方法 block中會(huì)將解析的數(shù)據(jù)傳遞出來
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray, NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths,NSArray<NSNumber *>* heights))dataBlock{
 //通過文件的url來將gif文件讀取為圖片數(shù)據(jù)引用
 CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
 //獲取gif文件中圖片的個(gè)數(shù)
 size_t count = CGImageSourceGetCount(source);
 //定義一個(gè)變量記錄gif播放一輪的時(shí)間
 float allTime=0;
 //存放所有圖片
 NSMutableArray * imageArray = [[NSMutableArray alloc]init];
 //存放每一幀播放的時(shí)間
 NSMutableArray * timeArray = [[NSMutableArray alloc]init];
 //存放每張圖片的寬度 (一般在一個(gè)gif文件中,所有圖片尺寸都會(huì)一樣)
 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添加一個(gè)設(shè)置gif圖內(nèi)容的方法:
-(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) {
  //添加幀動(dòng)畫
  CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
  NSMutableArray * times = [[NSMutableArray alloc]init];
  float currentTime = 0;
  //設(shè)置每一幀的時(shí)間占比
  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]];
  //設(shè)置循環(huán)
  animation.repeatCount= MAXFLOAT;
  //設(shè)置播放總時(shí)長
  animation.duration = totalTime;
  //Layer層添加
  [[(UIImageView *)__self layer]addAnimation:animation forKey:@"gifAnimation"];
 }];
}

@end

在加載gif的地方使用
導(dǎo)入 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 轉(zhuǎn) NSData
 //Gif 路徑
 NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"earthGif" ofType:@"gif"];
 //轉(zhuǎn)成NSData
 NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
 //初始化FLAnimatedImage對象
 FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:dataOfGif];
 //初始化FLAnimatedImageView對象
 FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
 //設(shè)置GIF圖片
 imageView.animatedImage = image;
 imageView.frame = CGRectMake(112, 342, 132, 102);
 [self.view addSubview:imageView];
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • iOS開發(fā)實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼封裝

    iOS開發(fā)實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼封裝

    這篇文章主要介紹了iOS開發(fā)實(shí)現(xiàn)隨機(jī)圖片驗(yàn)證碼封裝,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • iOS自定義滑桿效果

    iOS自定義滑桿效果

    這篇文章主要為大家詳細(xì)介紹了iOS自定義滑桿效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • iOS應(yīng)用設(shè)計(jì)模式開發(fā)中職責(zé)鏈(責(zé)任鏈)模式的實(shí)現(xiàn)解析

    iOS應(yīng)用設(shè)計(jì)模式開發(fā)中職責(zé)鏈(責(zé)任鏈)模式的實(shí)現(xiàn)解析

    這篇文章主要介紹了iOS應(yīng)用設(shè)計(jì)模式開發(fā)中職責(zé)鏈模式的相關(guān)實(shí)現(xiàn)解析,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2016-03-03
  • iOS實(shí)現(xiàn)scrollview上拉顯示Navbar下拉隱藏功能詳解

    iOS實(shí)現(xiàn)scrollview上拉顯示Navbar下拉隱藏功能詳解

    這篇文章主要給大家介紹了利用iOS實(shí)現(xiàn)scrollview上拉顯示Navbar下拉隱藏功能的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • iOS開發(fā)微信收款到賬語音提醒功能思路詳解

    iOS開發(fā)微信收款到賬語音提醒功能思路詳解

    這篇文章主要介紹了iOS開發(fā)微信收款到賬語音提醒功能思路詳解,需要的朋友可以參考下
    2017-09-09
  • iOS中多線程的入門使用教程(Swift)

    iOS中多線程的入門使用教程(Swift)

    這篇文章主要給大家介紹了關(guān)于iOS中多線程入門使用的相關(guān)資料,一個(gè)進(jìn)程中可以開啟多條線程,每條線程可以并行執(zhí)行不同的任務(wù),本文通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • iOS Swift邏輯運(yùn)算符示例總結(jié)

    iOS Swift邏輯運(yùn)算符示例總結(jié)

    運(yùn)算符是一個(gè)符號,用于告訴編譯器執(zhí)行一個(gè)數(shù)學(xué)或邏輯運(yùn)算,下面這篇文章主要給大家介紹了關(guān)于iOS Swift邏輯運(yùn)算符的相關(guān)資料,需要的朋友可以參考下
    2021-10-10
  • IOS開發(fā)中鍵盤輸入屏幕上移的解決方法

    IOS開發(fā)中鍵盤輸入屏幕上移的解決方法

    在IOS開法中經(jīng)常會(huì)遇到鍵盤遮擋屏幕的事情,經(jīng)常檔住下面的按鈕,下面小編給大家分享IOS開發(fā)中鍵盤輸入屏幕上移的解決方法,感興趣的朋友一起看看吧
    2016-10-10
  • iOS中GCD定時(shí)器詳解

    iOS中GCD定時(shí)器詳解

    ?CADisplayLink、NSTimer是基于RunLoop機(jī)制的,如果RunLoop的任務(wù)過于繁重,有可能會(huì)導(dǎo)致前兩個(gè)定時(shí)器不準(zhǔn)時(shí),這篇文章主要介紹了iOS中GCD定時(shí)器的相關(guān)知識,需要的朋友可以參考下
    2013-02-02
  • iOS開發(fā)使用UITableView制作N級下拉菜單的示例

    iOS開發(fā)使用UITableView制作N級下拉菜單的示例

    這篇文章主要介紹了iOS開發(fā)使用UITableView制作N級下拉菜單的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論

花莲市| 织金县| 平江县| 梧州市| 宾阳县| 三都| 东阳市| 普安县| 天气| 伊春市| 始兴县| 都江堰市| 卢氏县| 巴塘县| 瑞丽市| 营山县| 开江县| 罗源县| 盐亭县| 龙口市| 洪洞县| 卓尼县| 马尔康县| 南雄市| 缙云县| 吴堡县| 忻城县| 锡林浩特市| 长兴县| 甘肃省| 泗水县| 刚察县| 虹口区| 屯门区| 普宁市| 中阳县| 巩义市| 三台县| 呼图壁县| 拉萨市| 云林县|