iOS 展示 gif
2015-10-08 12:05
351 查看
gif 图 是多张依次有连续动作的图 顺时间展示的一种动态效果图 . 有的是均匀时间更换下一张 有的 则不是均匀时间变化
1. 那么 对于均匀 时间变化的gif图 比较适合 使用 iOS 系统自带方法 imageView 的动态展示图片的方法就好
如:
以上code 表达的是 10张图 在 1.5的时间 只执行一次动画展示(不循环重复) 所以关键 是这个 1.5秒 需要表达10张图 正常一次执行的时间即可出现效果比较好的动画
2. 不确定时间gif
上述方法 是按照 给定的常量时间 1.5s 执行gif 时间, 这样的弊端 是 如果把当前方法作为通用的类方法的话,这个1.5s 很不科学 . 所以 下边的方法是首先计算图片的总时间长度 .再执行动画.
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]; }
相关文章推荐
- iOS 关于NSDate的小结
- cocoaPods的更新
- ios开发编写规范总结
- iOS开发零基础教程之开发证书以及打包问题
- iOS抽象工厂模式-class cluster
- iOS xcode各种版本下载
- iOS中获取手机截屏的实现
- IOS视图跳转
- ios bitcode问题
- iOS 获取内购凭据
- iOS 9 之 3D touch
- iOS开发- 三步快速集成社交化分享工具ShareSDK
- IOS9:如何在 iOS 9 中创建 Stack View
- iOS设备相关信息获取
- IOS第三方类 播放音频 STKAudioPlayer
- iOS Xcode7 ‘/Users/**/Framework/SDKs/PolymerPay/Library/mobStat/lib**SDK.a(**ForSDK.o)’does not con
- iOS 获取设备类型
- iOS开发---集成ShareSDK实现第三方登录、分享、关注等功能。
- XCODE7 和IOS9适配后的一些问题。
- ios 滑块进度条百分比 显示进度