IOS-GIF Image Display in N ways (native + third-party)

Source: Internet
Author: User
Tags file url

IOS-GIF Image Display in N ways (native + third-party)

Native method:

1. UIWebView
Features: The loading speed is slightly longer, the performance is better, and the gif dynamic image playing is smoother.

// Dynamically display GIF images-WebView-(void) showGifImageWithWebView {// read GIF image data NSData * gifData = [NSData usage: [[NSBundle mainBundle] pathForResource: @ "earthGif" ofType: @ "gif"]; // UIWebView generated by UIWebView * imageWebView = [[UIWebView alloc] initWithFrame: CGRectMake (112,302,132,102)]; // the user cannot interact with imageWebView. userInteractionEnabled = NO; // loads gif data [imageWebView loadData: gifData MIMEType: @ "image/gif" textEncodingName: nil baseURL: nil]; // adds this gif control to the view [self. view addSubview: imageWebView];}

2. UIImagView
The loading method is faster, and the performance is inferior to that of UIWebView. Advantages: Easy scalability

1)
Add a category for UIImageView and add two methods.
UIImage + Tool
. H

# Import
  
   
@ Interface UIImageView (Tool)/** the method block for parsing gif file data will pass the parsed data */-(void) getGifImageWithUrk :( NSURL *) url returnData :( void (^) (NSArray
   
    
* ImageArray, NSArray
    
     
* TimeArray, CGFloat totalTime, NSArray
     
      
* Widths, NSArray
      
        * Heights) dataBlock;/** Add a method for setting gif image content for UIImageView: */-(void) yh_setImage :( NSURL *) imageUrl; @ end
      
     
    
   
  

. M

/// UIImageView + Tool. m // OneHelper // Created by qiuxuewei on 16/3/2. // Copyright? Qiu xuewei. All rights reserved. // # import "UIImageView + Tool. h" in 2016 // The ImageIO library will be introduced # import
  
   
@ Implementation UIImageView (Tool) // The method block for parsing gif file data will pass the parsed data-(void) getGifImageWithUrk :( NSURL *) url returnData :( void (^) (NSArray
   
    
* ImageArray, NSArray
    
     
* TimeArray, CGFloat totalTime, NSArray
     
      
* Widths, NSArray
      
        * Heights) dataBlock {// read the gif file as an image data reference through the File url CGImageSourceRef source = CGImageSourceCreateWithURL (CFURLRef) url, NULL ); // obtain the number of images in the gif file size_t count = CGImageSourceGetCount (source); // define a variable to record the time float allTime = 0 for the gif playback round; // store all images NSMutableArray * imageArray = [[NSMutableArray alloc] init]; // store the playback time of each frame NSMutableArray * timeArray = [[NSMutableArray alloc] init]; // store the width of each image (generally in a gif file, all images are of the same size) NSMutableArray * widthArray = [[NSMutableArray alloc] init]; // height of each image stored NSMutableArray * heightArray = [[NSMutableArray alloc] init]; // traverse for (size_t I = 0; I
       
         * ImageArray, NSArray
        
          * TimeArray, CGFloat totalTime, NSArray
         
           * Widths, NSArray
          
            * Heights) {// Add Frame animation CAKeyframeAnimation * animation = [CAKeyframeAnimation seconds: @ "contents"]; NSMutableArray * times = [[NSMutableArray alloc] init]; float currentTime = 0; // set the time proportion of each frame for (int I = 0; I
           
          
         
        
       
      
     
    
   
  

Use where 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 to NSData // Gif path NSString * pathForFile = [[NSBundle mainBundle] pathForResource: @ "earthGif" ofType: @ "gif"]; // convert it to NSData * dataOfGif = [NSData dataWithContentsOfFile: pathForFile]; // initialize the FLAnimatedImage object FLAnimatedImage * image = [FLAnimatedImage usage: dataOfGif]; // initialize the FLAnimatedImageView object FLAnimatedImageView * imageView = [[FLAnimatedImageView alloc] init]; // set the GIF image imageView. animatedImage = image; imageView. frame = CGRectMake (112,342,132,102); [self. view addSubview: imageView];}

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.