你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS Gif圖片展現N種方法(原生+第三方)

iOS Gif圖片展現N種方法(原生+第三方)

編輯:IOS開發綜合

本文分享了IOS Gif圖片展現N種方法,供年夜家參考,詳細內容以下

原生辦法:

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圖片展現N種方法(原生+第三方)】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved