IOS gif pictures show n ways (native + third party) _ios

Source: Internet
Author: User
Tags set time uikit

This article shares the iOS GIF image display n way, for your reference, the specific contents are as follows

Native method:

1.UIWebView
Features: The loading speed is slightly long, the performance is better, the playback GIF dynamic diagram is more fluent.

Animated display GIF picture-webview
-(void) showgifimagewithwebview{
 //Read GIF picture data
 nsdata *gifdata = [NSData Datawithcontentsoffile: [[NSBundle Mainbundle] pathforresource:@ "earthgif" oftype:@ "GIF"]];
 UIWebView generates
 UIWebView *imagewebview = [[UIWebView alloc] Initwithframe:cgrectmake (112, 302, 132, 102)];
 User is not interactive
 imagewebview.userinteractionenabled = NO;
 Loading GIF data
 [Imagewebview loaddata:gifdata mimetype:@ "Image/gif" Textencodingname:nil Baseurl:nil];
 View add this GIF control
 [Self.view Addsubview:imagewebview];
}

2.UIImagView
faster loading, less performance than UIWebView, advantages: easy to scale

1)
Add a uiimageview category (category), add two methods
Uiimage+tool
. h

#import <UIKit/UIKit.h>

@interface uiimageview (Tool)

/** parsing of GIF file data the parsed data is passed out in the Block
(void ) Getgifimagewithurk: (nsurl *) URL returndata: (void (^) (nsarray<uiimage *> * Imagearray,nsarray<nsnumber *> *timearray,cgfloat totaltime, Nsarray<nsnumber *>* widths, Nsarray<nsnumber *>*)) Heights;

/** adds a method for Uiimageview to set the contents of a GIF: * *
-(void) Yh_setimage: (Nsurl *) imageUrl;

@end

. m

UIIMAGEVIEW+TOOL.M//Onehelper////Created by Qiuxuewei on 16/3/2. COPYRIGHT©2016 year Chu Cowei.
All rights reserved. #import "uiimageview+tool.h"//To introduce ImageIO library #import <ImageIO/ImageIO.h> @implementation Uiimageview (Tool)// Methods for parsing GIF file data The parsed data is passed out-(void) Getgifimagewithurk: (nsurl *) URL returndata: (void (^) (nsarray<uiimage *> * Imagearray, Nsarray<nsnumber *>*timearray,cgfloat totaltime, Nsarray<nsnumber *>* widths,NSArray< NSNumber *>* Heights)) datablock{//through the URL of the file to read the GIF file as a picture data reference cgimagesourceref Source = Cgimagesourcecreatewithurl (
 cfurlref) URL, NULL);
 Gets the number of pictures in a gif file size_t count = cgimagesourcegetcount (source);
 Define a variable record GIF play round the time float alltime=0;
 Store all pictures nsmutablearray * Imagearray = [[Nsmutablearray alloc]init];
 Hold each frame play time nsmutablearray * Timearray = [[Nsmutablearray alloc]init];
 Store the width of each picture (generally in a GIF file, all picture dimensions will be the same) Nsmutablearray * Widtharray = [[Nsmutablearray alloc]init]; Store the height of each picture Nsmutablearray *Array = [[Nsmutablearray alloc]init];
  Traversal for (size_t i=0 i<count; i++) {cgimageref image = Cgimagesourcecreateimageatindex (source, I, NULL); [Imagearray addobject: (__bridge uiimage *)
  (image)];
  Cgimagerelease (image);
  Get picture information 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);
 ///Add a method for Uiimageview to set the contents of the 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) {//Add frame animation CAKEYFR
  Ameanimation *animation = [cakeyframeanimation animationwithkeypath:@ "Contents"];
  Nsmutablearray * times = [[Nsmutablearray alloc]init];
  float currenttime = 0; Set time per frame for (int i=0; i<imagearray.count; i++) {[Times Addobject:[nsnumber numberwithfloat:currenttime/total
   TIME]];
  Currenttime+=[timearray[i] Floatvalue];
  } [animation setkeytimes:times];
  [Animation Setvalues:imagearray];
  [Animation Settimingfunction:[camediatimingfunction Functionwithname:kcamediatimingfunctionlinear]];
  Set Loop animation.repeatcount= maxfloat;
  Set Playback total time animation.duration = TotalTime; Layer layer Add [[(Uiimageview *) __self Layer]addanimaTion:animation forkey:@ "Gifanimation"];
}];

 } @end

Use in the place where the GIF is loaded
Import 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];

}

Third party:
1.YLGIFImage
GitHub Link: 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 Link:https://github.com/Flipboard/FLAnimatedImage

-(void) showgifimagewithflanimatedimage{
 //gif nsdata
 //gif path
 nsstring *pathforfile = [[NSBundle Mainbundle] Pathforresource: @ "earthgif" oftype:@ "gif"];
 Turn into NSData
 nsdata *dataofgif = [NSData datawithcontentsoffile:pathforfile];
 Initialize Flanimatedimage object
 flanimatedimage *image = [Flanimatedimage animatedimagewithgifdata:dataofgif];
 Initializes the Flanimatedimageview object
 flanimatedimageview *imageview = [[Flanimatedimageview alloc] init];
 Set gif picture
 imageview.animatedimage = image;
 Imageview.frame = CGRectMake (112, 342, 132, 102);
 [Self.view Addsubview:imageview];
}

The above is the entire content of this article, I hope to help you learn.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.