iOS加载动态图的两种方法

view = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 100,
100)];

NSString *imagePath =[[NSBundle
mainBundle] pathForResource:@"csjg"
ofType:@"gif"];

CGImageSourceRef  cImageSource = CGImageSourceCreateWithURL((__bridge
CFURLRef)[NSURL
fileURLWithPath:imagePath],
NULL);

size_t imageCount = CGImageSourceGetCount(cImageSource);

NSMutableArray *images = [[NSMutableArray
alloc] initWithCapacity:imageCount];

NSMutableArray *times = [[NSMutableArray
alloc] initWithCapacity:imageCount];

NSMutableArray *keyTimes = [[NSMutableArray
alloc] initWithCapacity:imageCount];

float totalTime = 0;

for (size_t i =
0; i < imageCount; i++) {

CGImageRef cgimage= CGImageSourceCreateImageAtIndex(cImageSource, i,
NULL);

[images
addObject:(__bridge
id)cgimage];

CGImageRelease(cgimage);

NSDictionary *properties = (__bridge
NSDictionary *)CGImageSourceCopyPropertiesAtIndex(cImageSource, i,
NULL);

NSDictionary *gifProperties = [properties
valueForKey:(__bridge
NSString *)kCGImagePropertyGIFDictionary];

NSString *gifDelayTime = [gifProperties valueForKey:(__bridge
NSString* )kCGImagePropertyGIFDelayTime];

[times
addObject:gifDelayTime];

totalTime += [gifDelayTime
floatValue];

//        _size.width = [[properties valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue];

//        _size.height = [[properties valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue];

}

float currentTime = 0;

for (size_t i =
0; i < times.count; i++) {

float keyTime = currentTime / totalTime;

[keyTimes
addObject:[NSNumber
numberWithFloat:keyTime]];

currentTime += [[times
objectAtIndex:i] floatValue];

}

CAKeyframeAnimation *animation = [CAKeyframeAnimation
animationWithKeyPath:@"contents"];

[animation setTimingFunction:[CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear]];

[animation
setValues:images];

[animation
setKeyTimes:keyTimes];

animation.duration = totalTime;

animation.repeatCount =
HUGE_VALF;

[view.layer
addAnimation:animation
forKey:@"gifAnimation"];

//

//  GifView.h

//  GIFViewer

//

//  Created by xToucher04 on 11-11-9.

//  Copyright 2011 Toucher. All rights reserved.

//

#import <UIKit/UIKit.h>

#import <ImageIO/ImageIO.h>

@interface GifView :
UIView {

CGImageSourceRef gif;

NSDictionary *gifProperties;

size_t index;

size_t count;

NSTimer *timer;

}

- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath;

- (id)initWithFrame:(CGRect)frame data:(NSData *)_data;

@end

//

//  GifView.m

//  GIFViewer

//

//  Created by xToucher04 on 11-11-9.

//  Copyright 2011 Toucher. All rights reserved.

//

#import "GifView.h"

#import <QuartzCore/QuartzCore.h>

@implementation GifView

- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{

self = [super
initWithFrame:frame];

if (self) {

gifProperties = [[NSDictionary
dictionaryWithObject:[NSDictionary
dictionaryWithObject:[NSNumber
numberWithInt:0]
forKey:(NSString *)kCGImagePropertyGIFLoopCount]

forKey:(NSString *)kCGImagePropertyGIFDictionary]
retain];

gif =
CGImageSourceCreateWithURL((CFURLRef)[NSURL
fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);

count =CGImageSourceGetCount(gif);

timer = [NSTimer
scheduledTimerWithTimeInterval:0.12
target:self
selector:@selector(play)
userInfo:nil
repeats:YES];

[timer
fire];

}

return
self;

}

- (id)initWithFrame:(CGRect)frame data:(NSData *)_data{

self = [super
initWithFrame:frame];

if (self) {

gifProperties = [[NSDictionary
dictionaryWithObject:[NSDictionary
dictionaryWithObject:[NSNumber
numberWithInt:0]
forKey:(NSString *)kCGImagePropertyGIFLoopCount]

forKey:(NSString *)kCGImagePropertyGIFDictionary]
retain];

//
gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);

gif =
CGImageSourceCreateWithData((CFDataRef)_data, (CFDictionaryRef)gifProperties);

count =CGImageSourceGetCount(gif);

timer = [NSTimer
scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(play)
userInfo:nil
repeats:YES];

[timer
fire];

}

return
self;

}

-(void)play

{

index ++;

index =
index%count;

CGImageRef ref =
CGImageSourceCreateImageAtIndex(gif,
index, (CFDictionaryRef)gifProperties);

self.layer.contents = (id)ref;

CFRelease(ref);

}

-(void)removeFromSuperview

{

NSLog(@"removeFromSuperview");

[timer
invalidate];

timer =
nil;

[super
removeFromSuperview];

}

- (void)dealloc {

NSLog(@"dealloc");

CFRelease(gif);

[gifProperties
release];

[super
dealloc];

}

@end

使用的时候:

GifView *pathView =[[GifView
alloc] initWithFrame:CGRectMake(100,
0, 100,
100)
filePath:[[NSBundle
mainBundle] pathForResource:@"csjg"
ofType:@"gif"]];

时间: 2024-08-10 01:53:01

iOS加载动态图的两种方法的相关文章

xml文件 加载properties文件的两种方法与注意事项

1.遇到的问题: 配置redisSpringContext.xml 时,遇到 properties加载失败,提示BeanDefinitionStoreException  和   java.lang.IllegalArgumentException: Could not resolve placeholder 'xxx'  , 找了很久都找不到原因,文件路径和键名都没写错,加载语法也没有写错...这就很难受了. 直到检查web.xml文件,,我猜测,是不是因为 springcontext-*.x

动态加载JS脚本的4种方法

要实现动态加载JS脚本有4种方法: 1.直接document.write <script language="javascript"> document.write("<script src='test.js'><\/script>"); </script>   2.动态改变已有script的src属性 <script src='' id="s1"></script> <

Spring加载properties文件的两种方式

在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可,不需要修改源代码,这样更加方便.在Spring中也可以这么做,而且Spring有两种加载properties文件的方式:基于xml方式和基于注解方式.下面分别讨论下这两种方式. 1. 通过xml方式加载properties文件 我们以Spring实例化dataSource为例,我们一般会在beans

VC6.0加载lib文件的三种方法

MFC编写程序,都要用到动态链接库,MFC相关的动态库有MFCD42和MFC42等,MFC框架程序已经自动加载,那么如何引入第三方的动态链接库到工程中呢? 静态链接库是要先把程序中所需要使用的函数编译成机器码,保存在*.lib文件中.编译器会去*.lib中找出所需要的函数,并把这些函数的机器码复制一份,放在可执行文件中.将lib库引入工程,有下面3种方法. 第一种方法,lib文件直接加入到工程文件列表中. 在VC中打开“File View”一页,选中工程名,单击鼠标右键,然后在弹出的快捷菜单中选

HTML 页面加载 Flash 插件的几种方法

前言 之所以写这篇文章,主要是因为组长给提的一个新的需求--使用浏览器调用电脑的摄像头,来实现即时拍照的功能.在网上查了很多资料,由于这样那样的原因,最终选择了使用flash插件来调用pc的摄像头.当然,这个需求是基于B/S架构的,因此,就在想怎么把它嵌入到前端的HTML页面中. 题外话 当然,这里还没有考虑到封装,主要是先以实现为目的,后续工作再根据业务进行抽象,封装成通用的组件.好了,废话不多说,看重点. 嵌入插件 使用 object 和 embed 标签 代码展示 <span style=

OC中动态创建可变数组的问题.有一个数组,数组中有13个元素,先将该数组进行分组,每3个元素为一组,分为若干组,最后用一个数组统一管理这些分组.(要动态创建数组).两种方法

<span style="font-size:24px;">//////第一种方法 // NSMutableArray *arr = [NSMutableArray array]; // for (int i = 0; i < 13; i ++) { // [arr addObject:[NSString stringWithFormat:@"lanou%d",i + 1]]; // } // NSLog(@"%@",arr);

加载图片时的2种方法

1,仅加载图片,图像数据不会缓存. NSString *path = [[NSBundle mainBundle] pathForResource:@”icon” ofType:@”png”]; UIImage *image = [UIImage imageWithContentsOfFile:path]; 或 NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:“png”]; NSData *i

SpringBoot中使用Spring Data Jpa 实现简单的动态查询的两种方法

首先谢谢大佬的简书文章:http://www.jianshu.com/p/45ad65690e33# 这篇文章中讲的是spring中使用spring data jpa,使用了xml配置文件.我现在使用的是spring boot ,没有了xml文件配置就方便多了.我同样尝试了两种方式,也都是简单的查询,需要更复杂的查询,还需要我研究研究.往下看,需要先配置springboot的开发环境,需要大致了解springboot,这里可以看下面两篇文章: springboot 项目新建 springboot

UIImage加载本地图片的两种方式

UIImage加载图片方式一般有两种: (1)imagedNamed初始化:默认加载图片成功后会内存中缓存图片,这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象.如果缓存中没有找到相应的图片对象,则从指定地方加载图片然后缓存对象,并返回这个图片对象. (2)imageWithContentsOfFile初始化:则仅只加载图片,不缓存. 大量使用imageNamed方式会在不需要缓存的地方额外增加开销CPU的时间来做这件事.当应用程序需要加载一张比较大的图片并且使用一次性,那么其实是没有