iOS 展示 gif

gif 图 是多张依次有连续动作的图 顺时间展示的一种动态效果图 .   有的是均匀时间更换下一张  有的 则不是均匀时间变化

1. 那么 对于均匀 时间变化的gif图 比较适合 使用 iOS 系统自带方法 imageView 的动态展示图片的方法就好

如:

NSMutableArray *array=[NSMutableArray arrayWithCapacity:0];
for(int i=1;i   < 10;i++)
 {
   NSString *str=[NSString stringWithFormat:@"Nav_Bg%d.png",i];
UIImage
*image=[UIImage imageNamed:str];
[array addObject:image]; } UIImageView
*imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; imageView.animationImages=array; imageView.animationDuration=1.5;//duration :持续 持续时间 imageView.animationRepeatCount=1 ;//重复次数 [self .window addSubview:imageView]; [imageView startAnimating];

以上code  表达的是 10张图 在 1.5的时间 只执行一次动画展示(不循环重复)   所以关键 是这个 1.5秒 需要表达10张图 正常一次执行的时间即可出现效果比较好的动画

2.  不确定时间gif

上述方法 是按照 给定的常量时间 1.5s 执行gif 时间, 这样的弊端 是 如果把当前方法作为通用的类方法的话,这个1.5s 很不科学 . 所以 下边的方法是首先计算图片的总时间长度 .再执行动画.

#import <UIKit/UIKit.h>
@interface MotionActiveAgeGif : UIView
{
    UIImageView *gitView;
}
@property (nonatomic ,strong) UIImageView *gitView;
@property (nonatomic ,strong)UIImageView * lastGifView;
//@property (nonatomic ,strong)NSString * gifName;
- (instancetype)initWithName:(NSString *)gifName andFrame:(CGRect)frame ;
@end



#import "MotionActiveAgeGif.h"

#import <ImageIO/ImageIO.h>
#import <QuartzCore/CoreAnimation.h>

//#define DuringTime  1.5f
@interface MotionActiveAgeGif ()
{
    float totalTime ;
    CADisplayLink *displayLink ;
}
@end
@implementation MotionActiveAgeGif
@synthesize gitView ;
@synthesize lastGifView ;
- (instancetype)initWithName:(NSString *)gifName andFrame:(CGRect)frame
{
    self = [super init];
    if (self) {
        totalTime = 0;
        gitView = [[UIImageView alloc]initWithFrame:frame];
        lastGifView =  [[UIImageView alloc]initWithFrame:gitView.frame];
        NSURL *url = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"%@.gif",gifName] withExtension:nil];
        CGImageSourceRef csf = CGImageSourceCreateWithURL((__bridge CFTypeRef) url, NULL);
        size_t const count = CGImageSourceGetCount(csf);
        UIImage *frames[count];
        CGImageRef images[count];
        
        NSMutableArray *delayTimes = [NSMutableArray array];
        for (size_t i = 0; i < count; ++i) {
            images[i] = CGImageSourceCreateImageAtIndex(csf, i, NULL);
            UIImage *image =[[UIImage alloc] initWithCGImage:images[i]];
            lastGifView.image = image ;
            frames[i] = image;
            
            //CFBridgingRelease 给予arc所有权
            NSDictionary *dict = (NSDictionary*)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(csf, i, NULL));
            NSLog(@"kCGImagePropertyGIFDictionary %@", [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary]);
            //
            NSDictionary *gifDict = [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary];
            [delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]];
            
            if (totalTime >= 0)
            {
                totalTime = totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFUnclampedDelayTime] floatValue];
            }
            NSLog(@"%lf",totalTime);

            CFRelease(images[i]);
        }
        UIImage *const animation = [UIImage animatedImageWithImages:[NSArray arrayWithObjects:frames count:count] duration:totalTime];
        gitView.image = animation;
        gitView.animationRepeatCount =1;  //动画重复次数1  不起作用
        [gitView startAnimating];
        [self addSubview:gitView];
        self.frame = gitView.frame;
        
        [self performSelector:@selector(stopGifView) withObject:nil afterDelay:totalTime];
        CFRelease(csf);
    }
    return self;
}
- (void)stopGifView
{
    [gitView removeFromSuperview];
    [self addSubview:lastGifView];
}

 

原文地址:https://www.cnblogs.com/someonelikeyou/p/4637134.html