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 很不科学 . 所以 下边的方法是首先计算图片的总时间长度 .再执行动画.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #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]; } |
posted on 2015-10-08 12:05 ACM_Someone like you 阅读(221) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
2014-10-08 IOS UIlabel 、UIButton添加下划线