分享

iOS gif显示

 叹落花 2015-01-16
  1. //  
  2. //  UIImageView.h  
  3. //  UIKit  
  4. //  
  5. //  Copyright (c) 2006-2012, Apple Inc. All rights reserved.  
  6. //  
  7.   
  8. #import <Foundation/Foundation.h>  
  9. #import <UIKit/UIView.h>  
  10. #import <UIKit/UIKitDefines.h>  
  11.   
  12. @class UIImage;  
  13.   
  14. NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImageView : UIView {  
  15.   @private  
  16.     id _storage;  
  17. }  
  18.   
  19. - (id)initWithImage:(UIImage *)image;  
  20. - (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage NS_AVAILABLE_IOS(3_0);  
  21.   
  22. @property(nonatomic,retain) UIImage *image;                                                     // default is nil  
  23. @property(nonatomic,retain) UIImage *highlightedImage NS_AVAILABLE_IOS(3_0);      // default is nil  
  24. @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled;               // default is NO  
  25.   
  26. @property(nonatomic,getter=isHighlighted) BOOL highlighted NS_AVAILABLE_IOS(3_0); // default is NO  
  27.   
  28. // these allow a set of images to be animated. the array may contain multiple copies of the same  
  29.   
  30. @property(nonatomic,copy) NSArray *animationImages;            // The array must contain UIImages. Setting hides the single image. default is nil  
  31. @property(nonatomic,copy) NSArray *highlightedAnimationImages NS_AVAILABLE_IOS(3_0);            // The array must contain UIImages. Setting hides the single image. default is nil  
  32.   
  33. @property(nonatomic) NSTimeInterval animationDuration;         // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)  
  34. @property(nonatomic) NSInteger      animationRepeatCount;      // 0 means infinite (default is 0)  
  35.   
  36. - (void)startAnimating;  
  37. - (void)stopAnimating;  
  38. - (BOOL)isAnimating;  
  39.   
  40. @end  

相信看了上面的代码,大家都晓得了。这是imageview中的方法。

其中image,highlightedImage 大家都清楚就是设置图片。

那么同样,animationImages, highlightedAnimationImages  这两个是数组,其实就是每一帧的图片加到里面就可以了。

如果大家手头上刚好有一个gif的所有帧。那么就可以写下gif来看看了。当然最简单的还是webview

  1. [_imageview_gif setAnimationImages:[NSArray arrayWithObjects:[UIImage imageNamed:@"gif_01"],[UIImage imageNamed:@"gif_02"],[UIImage imageNamed:@"gif_03"], nil]];  
  2.     [_imageview_gif setContentMode:UIViewContentModeCenter];  
  3.     [_imageview_gif setAnimationDuration:0.3];  
  4.     [_imageview_gif startAnimating];  

2.如果是网上的图片呢?既然知道显示的原理,那么。现在我们只需要把gif的每帧取出来就可以了

下面直接贴代码了。有比较详细的注释。

  1. - (void)create  
  2. {  
  3.     /* 
  4.      *path :    本地gif路径 
  5.      *NSLog:    /Users/xx/Library/Application Support/iPhone Simulator/6.1/Applications/423061D3-006A-4F61-9E9E-E0D889728D87/AllTest.app/test.gif 
  6.      *data :    取得这个gif 
  7.      */  
  8.     NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];  
  9.     NSData *data = [NSData dataWithContentsOfFile:path];  
  10.     /* 
  11.      *gifLoopCount  : 设置一个gif的循环属性 ,值为0 
  12.      *NSLog:     
  13.      *{ 
  14.      *   LoopCount = 0; 
  15.      *} 
  16.      */  
  17.     NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObjectsAndKeys:  
  18.                                   [NSNumber numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil  
  19.                                   ];  
  20.     /* 
  21.      *创建gif属性,可以看到只有一个属性,个人理解为: 
  22.      *      因为要得到gif,就必须创建gif,而创建就要有属性,所以这里设置了一个无用的属性,无限循环。用于创建gif,并且不覆盖原属性 
  23.      *NSLog: 
  24.      *{ 
  25.      *  "{GIF}" =     { 
  26.      *      LoopCount = 0; 
  27.      *  }; 
  28.      *} 
  29.      */  
  30.     NSDictionary * gifProperties = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary] ;  
  31.     /* 
  32.      *根据属性 还有data 得到gif,并存在CGImageSourceRef中    
  33.      *NSLog:    <CGImageSource 0x8d58740 [0x243a4d8]> 
  34.      *CFDictionaryRef ,得到gif的真正属性. 
  35.      *NSLog: 
  36.      *{ 
  37.      *    ColorModel = RGB; 
  38.      *    Depth = 8;// 
  39.      *    HasAlpha = 1; 
  40.      *    PixelHeight = 22; 
  41.      *    PixelWidth = 22; 
  42.      *    "{GIF}" =     { 
  43.      *        DelayTime = "0.1"; 
  44.      *        UnclampedDelayTime = "0.1"; 
  45.      *    }; 
  46.      *} 
  47.      *为什么要保存原属性呢。因为我们需要gif的原始延迟时间 
  48.      */  
  49.     CGImageSourceRef gif = CGImageSourceCreateWithData((__bridge  CFDataRef)(data), (__bridge  CFDictionaryRef)gifProperties);  
  50.     CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));  
  51.     /* 
  52.      *count :  gif的张数 
  53.      *NSLog: 
  54.      *(NSInteger) count = 19 
  55.      */  
  56.     NSInteger count =CGImageSourceGetCount(gif);  
  57.     /* 
  58.      *delay:    得到原始延迟时间 
  59.      *NSLog 
  60.      *{ 
  61.      *    DelayTime = "0.1"; 
  62.      *    UnclampedDelayTime = "0.1"; 
  63.      *} 
  64.      */  
  65.     CFDictionaryRef  gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);  
  66.     //[gifprops objectForKey:(NSString *)kCGImagePropertyGIFDictionary];  
  67.       
  68.     NSNumber * delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime);  
  69.     //[gifDic objectForKey:(NSString *)kCGImagePropertyGIFDelayTime];  
  70.     NSNumber * w = CFDictionaryGetValue(gifprops, @"PixelWidth");  
  71.     NSNumber * h =CFDictionaryGetValue(gifprops, @"PixelHeight");  
  72.     /* 
  73.      *记算播放完一次gif需要多长时间。imageview播放动画需要这个时间 
  74.      */  
  75.     NSTimeInterval totalDuration  = delay.doubleValue * count;  
  76.     CGFloat pixelWidth = w.intValue;  
  77.     CGFloat pixelHeight = h.intValue;  
  78.       
  79.     /* 
  80.      *循环取得gif中的图片然后加到数组中。 
  81.      */  
  82.     NSMutableArray *images = [[NSMutableArray alloc] init];  
  83.     for(int index=0;index<count;index++)  
  84.     {  
  85.         CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil);  
  86.         UIImage *img = [UIImage imageWithCGImage:ref];  
  87.         [images addObject:img];  
  88.         CFRelease(ref);  
  89.     }  
  90.       
  91.     CFRelease(gifprops);  
  92.     CFRelease(gif);  
  93.     /* 
  94.      *记得释放 
  95.      */  
  96.     [_imageview_gif setBounds:CGRectMake(0, 0, pixelWidth, pixelHeight)];  
  97.     [_imageview_gif setAnimationImages:images];  
  98.     [_imageview_gif setAnimationDuration:totalDuration];  
  99.     [_imageview_gif startAnimating];  
  100.       
  101. }  







    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多