- //
- // UIImageView.h
- // UIKit
- //
- // Copyright (c) 2006-2012, Apple Inc. All rights reserved.
- //
-
- #import <Foundation/Foundation.h>
- #import <UIKit/UIView.h>
- #import <UIKit/UIKitDefines.h>
-
- @class UIImage;
-
- NS_CLASS_AVAILABLE_IOS(2_0) @interface UIImageView : UIView {
- @private
- id _storage;
- }
-
- - (id)initWithImage:(UIImage *)image;
- - (id)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage NS_AVAILABLE_IOS(3_0);
-
- @property(nonatomic,retain) UIImage *image; // default is nil
- @property(nonatomic,retain) UIImage *highlightedImage NS_AVAILABLE_IOS(3_0); // default is nil
- @property(nonatomic,getter=isUserInteractionEnabled) BOOL userInteractionEnabled; // default is NO
-
- @property(nonatomic,getter=isHighlighted) BOOL highlighted NS_AVAILABLE_IOS(3_0); // default is NO
-
- // these allow a set of images to be animated. the array may contain multiple copies of the same
-
- @property(nonatomic,copy) NSArray *animationImages; // The array must contain UIImages. Setting hides the single image. default is nil
- @property(nonatomic,copy) NSArray *highlightedAnimationImages NS_AVAILABLE_IOS(3_0); // The array must contain UIImages. Setting hides the single image. default is nil
-
- @property(nonatomic) NSTimeInterval animationDuration; // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
- @property(nonatomic) NSInteger animationRepeatCount; // 0 means infinite (default is 0)
-
- - (void)startAnimating;
- - (void)stopAnimating;
- - (BOOL)isAnimating;
-
- @end
相信看了上面的代码,大家都晓得了。这是imageview中的方法。
其中image,highlightedImage 大家都清楚就是设置图片。
那么同样,animationImages, highlightedAnimationImages 这两个是数组,其实就是每一帧的图片加到里面就可以了。
如果大家手头上刚好有一个gif的所有帧。那么就可以写下gif来看看了。当然最简单的还是webview
- [_imageview_gif setAnimationImages:[NSArray arrayWithObjects:[UIImage imageNamed:@"gif_01"],[UIImage imageNamed:@"gif_02"],[UIImage imageNamed:@"gif_03"], nil]];
- [_imageview_gif setContentMode:UIViewContentModeCenter];
- [_imageview_gif setAnimationDuration:0.3];
- [_imageview_gif startAnimating];
2.如果是网上的图片呢?既然知道显示的原理,那么。现在我们只需要把gif的每帧取出来就可以了
下面直接贴代码了。有比较详细的注释。
- - (void)create
- {
- /*
- *path : 本地gif路径
- *NSLog: /Users/xx/Library/Application Support/iPhone Simulator/6.1/Applications/423061D3-006A-4F61-9E9E-E0D889728D87/AllTest.app/test.gif
- *data : 取得这个gif
- */
- NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];
- NSData *data = [NSData dataWithContentsOfFile:path];
- /*
- *gifLoopCount : 设置一个gif的循环属性 ,值为0
- *NSLog:
- *{
- * LoopCount = 0;
- *}
- */
- NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithInt:0] , (NSString *)kCGImagePropertyGIFLoopCount,nil
- ];
- /*
- *创建gif属性,可以看到只有一个属性,个人理解为:
- * 因为要得到gif,就必须创建gif,而创建就要有属性,所以这里设置了一个无用的属性,无限循环。用于创建gif,并且不覆盖原属性
- *NSLog:
- *{
- * "{GIF}" = {
- * LoopCount = 0;
- * };
- *}
- */
- NSDictionary * gifProperties = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary] ;
- /*
- *根据属性 还有data 得到gif,并存在CGImageSourceRef中
- *NSLog: <CGImageSource 0x8d58740 [0x243a4d8]>
- *CFDictionaryRef ,得到gif的真正属性.
- *NSLog:
- *{
- * ColorModel = RGB;
- * Depth = 8;//
- * HasAlpha = 1;
- * PixelHeight = 22;
- * PixelWidth = 22;
- * "{GIF}" = {
- * DelayTime = "0.1";
- * UnclampedDelayTime = "0.1";
- * };
- *}
- *为什么要保存原属性呢。因为我们需要gif的原始延迟时间
- */
- CGImageSourceRef gif = CGImageSourceCreateWithData((__bridge CFDataRef)(data), (__bridge CFDictionaryRef)gifProperties);
- CFDictionaryRef gifprops =(CGImageSourceCopyPropertiesAtIndex(gif,0,NULL));
- /*
- *count : gif的张数
- *NSLog:
- *(NSInteger) count = 19
- */
- NSInteger count =CGImageSourceGetCount(gif);
- /*
- *delay: 得到原始延迟时间
- *NSLog
- *{
- * DelayTime = "0.1";
- * UnclampedDelayTime = "0.1";
- *}
- */
- CFDictionaryRef gifDic = CFDictionaryGetValue(gifprops, kCGImagePropertyGIFDictionary);
- //[gifprops objectForKey:(NSString *)kCGImagePropertyGIFDictionary];
-
- NSNumber * delay = CFDictionaryGetValue(gifDic, kCGImagePropertyGIFDelayTime);
- //[gifDic objectForKey:(NSString *)kCGImagePropertyGIFDelayTime];
- NSNumber * w = CFDictionaryGetValue(gifprops, @"PixelWidth");
- NSNumber * h =CFDictionaryGetValue(gifprops, @"PixelHeight");
- /*
- *记算播放完一次gif需要多长时间。imageview播放动画需要这个时间
- */
- NSTimeInterval totalDuration = delay.doubleValue * count;
- CGFloat pixelWidth = w.intValue;
- CGFloat pixelHeight = h.intValue;
-
- /*
- *循环取得gif中的图片然后加到数组中。
- */
- NSMutableArray *images = [[NSMutableArray alloc] init];
- for(int index=0;index<count;index++)
- {
- CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, nil);
- UIImage *img = [UIImage imageWithCGImage:ref];
- [images addObject:img];
- CFRelease(ref);
- }
-
- CFRelease(gifprops);
- CFRelease(gif);
- /*
- *记得释放
- */
- [_imageview_gif setBounds:CGRectMake(0, 0, pixelWidth, pixelHeight)];
- [_imageview_gif setAnimationImages:images];
- [_imageview_gif setAnimationDuration:totalDuration];
- [_imageview_gif startAnimating];
-
- }
|