UIImageView帧动画
2016-04-22 09:48
106 查看
我们在使用UIImageView帧动画时会碰到加载到内存的图片不会自动释放,占用很多的内存,这时我们可能使用 UIImage imageWithContentsOfFile 并配合 imageView.animationImages = nil; 来清理不用的缓存动画图片。具体如下:
UIImageView帧动画相关属性和方法:
需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)
@property(nonatomic,copy) NSArray *animationImages;
帧动画的持续时间
@property(nonatomic) NSTimeInterval animationDuration;
帧动画的执行次数(默认是无限循环)
@property(nonatomic) NSInteger animationRepeatCount;
开始执行帧动画
- (void)startAnimating;
停止执行帧动画
- (void)stopAnimating;
是否正在执行帧动画
- (BOOL)isAnimating;
例:加载动画图片的方式
// 1.加载所有的动画图片
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
if (self.imageView.isAnimating) return;
// 1.加载所有的动画图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i
// 计算文件名
NSString *filename = [NSString stringWithFormat:@"%@_d.jpg", name, i];
// 加载图片
// imageNamed: 有内存缓存直到程序退出才释放(传入文件名)
// UIImage *image = [UIImage imageNamed:filename];
// imageWithContentsOfFile: 没有缓存,自动释放(传入文件的全路径)
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
// 添加图片到数组中
[images addObject:image];
}
self.imageView.animationImages = images;
// 2.设置播放次数(1次)
self.imageView.animationRepeatCount = 1;
// 3.设置播放时间
self.imageView.animationDuration = images.count * 0.05;
[self.imageViewstartAnimating];
// 4.动画放完1秒后清除内存
CGFloat delay = self.tom.animationDuration;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
- (IBAction)onButtonClickHandler:(id)sender
{
UIImageView *heartImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100.0, 100.0, 50.0, 50.0)];
[self.view addSubview:heartImageView];
NSMutableArray *images = [[NSMutableArrayalloc]initWithCapacity:7];
for (int i=1; i<=7; i++) {
[images addObject:[UIImageimageNamed:[NSStringstringWithFormat:@"xxx%d.png",i]]];
}
heartImageView.animationImages = images;
heartImageView.animationDuration = 0.4 ;
heartImageView.animationRepeatCount = 1;
[heartImageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:heartImageView.animationDuration
target:self
selector:@selector(onFrameAnimationFinished:)
userInfo:heartImageView
repeats:NO];
}
- (void)onFrameAnimationFinished:(NSTimer *)timer{
UIImageView * imageView = (UIImageView *)[timeruserInfo];
[imageView removeFromSuperview];
}
(2015-02-24 13:57:18)
转载▼
//播放动画
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
if (self.tom.isAnimating) return;
// 1.加载所有的动画图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i
// 计算文件名
NSString *filename = [NSString stringWithFormat:@"%@_d.jpg", name, i];
// 加载图片
//缓存就是
// imageNamed: 有缓存机制(传入文件名);好处是快,坏处就是占内存
// UIImage *image = [UIImage imageNamed:filename];
// imageWithContentsOfFile: 没有缓存(传入文件的全路径)
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
// 添加图片到数组中
[images addObject:image];
}
self.tom.animationImages = images;
// 2.设置播放次数(1次)
self.tom.animationRepeatCount = 1;
// 3.设置播放时间
self.tom.animationDuration = images.count * 0.05;
[self.tom startAnimating];
// 4.动画放完1秒后清除内存
CGFloat delay = self.tom.animationDuration + 1.0;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
// [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}
//- (void)clearCache
//{
//// self.tom.animationImages = nil;
//
// [self.tom setAnimationImages:nil];
//}
UIImageView帧动画相关属性和方法:
需要播放的序列帧图片数组(里面都是UIImage对象,会按顺序显示里面的图片)
@property(nonatomic,copy) NSArray *animationImages;
帧动画的持续时间
@property(nonatomic) NSTimeInterval animationDuration;
帧动画的执行次数(默认是无限循环)
@property(nonatomic) NSInteger animationRepeatCount;
开始执行帧动画
- (void)startAnimating;
停止执行帧动画
- (void)stopAnimating;
是否正在执行帧动画
- (BOOL)isAnimating;
例:加载动画图片的方式
// 1.加载所有的动画图片
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
if (self.imageView.isAnimating) return;
// 1.加载所有的动画图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i
// 计算文件名
NSString *filename = [NSString stringWithFormat:@"%@_d.jpg", name, i];
// 加载图片
// imageNamed: 有内存缓存直到程序退出才释放(传入文件名)
// UIImage *image = [UIImage imageNamed:filename];
// imageWithContentsOfFile: 没有缓存,自动释放(传入文件的全路径)
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
// 添加图片到数组中
[images addObject:image];
}
self.imageView.animationImages = images;
// 2.设置播放次数(1次)
self.imageView.animationRepeatCount = 1;
// 3.设置播放时间
self.imageView.animationDuration = images.count * 0.05;
[self.imageViewstartAnimating];
// 4.动画放完1秒后清除内存
CGFloat delay = self.tom.animationDuration;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
}
- (IBAction)onButtonClickHandler:(id)sender
{
UIImageView *heartImageView = [[UIImageViewalloc]initWithFrame:CGRectMake(100.0, 100.0, 50.0, 50.0)];
[self.view addSubview:heartImageView];
NSMutableArray *images = [[NSMutableArrayalloc]initWithCapacity:7];
for (int i=1; i<=7; i++) {
[images addObject:[UIImageimageNamed:[NSStringstringWithFormat:@"xxx%d.png",i]]];
}
heartImageView.animationImages = images;
heartImageView.animationDuration = 0.4 ;
heartImageView.animationRepeatCount = 1;
[heartImageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:heartImageView.animationDuration
target:self
selector:@selector(onFrameAnimationFinished:)
userInfo:heartImageView
repeats:NO];
}
- (void)onFrameAnimationFinished:(NSTimer *)timer{
UIImageView * imageView = (UIImageView *)[timeruserInfo];
[imageView removeFromSuperview];
}
UIImageView帧动画的使用以及缓存的问题
(2015-02-24 13:57:18)转载▼
标签: | 分类: ios开发 |
- (void)runAnimationWithCount:(int)count name:(NSString *)name
{
if (self.tom.isAnimating) return;
// 1.加载所有的动画图片
NSMutableArray *images = [NSMutableArray array];
for (int i = 0; i
// 计算文件名
NSString *filename = [NSString stringWithFormat:@"%@_d.jpg", name, i];
// 加载图片
//缓存就是
// imageNamed: 有缓存机制(传入文件名);好处是快,坏处就是占内存
// UIImage *image = [UIImage imageNamed:filename];
// imageWithContentsOfFile: 没有缓存(传入文件的全路径)
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:filename ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
// 添加图片到数组中
[images addObject:image];
}
self.tom.animationImages = images;
// 2.设置播放次数(1次)
self.tom.animationRepeatCount = 1;
// 3.设置播放时间
self.tom.animationDuration = images.count * 0.05;
[self.tom startAnimating];
// 4.动画放完1秒后清除内存
CGFloat delay = self.tom.animationDuration + 1.0;
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay];
// [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
}
//- (void)clearCache
//{
//// self.tom.animationImages = nil;
//
// [self.tom setAnimationImages:nil];
//}
相关文章推荐
- 【对照Android与IOS开发系列】之 Activity与UIViewController
- mongo客户端mongo VUE增删改查
- 常见移动设备的 CSS3 Media Query 整理(iPhone/iPad/Galaxy/HTC One etc.)
- 2016.04.22,英语,《Vocabulary Builder》Unit 17
- 一个类似MIUI中原生短信编辑功能
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
- pageContext.request.contextPath
- easyui datetimebox 如何只显示 月份,不显示具体的数据
- 关于EF分页查询报错(Count must have a non-negative value.)的解决方案
- 【代码笔记】UIScrollerView里有两个tableView
- 【代码笔记】iOS-UIScrollerView里有两个tableView
- String,StringBuffer与StringBuilder的区别??
- iOS开发系列--UITableView全面解析
- easyui中datagrid 部分参数说明
- 百度编辑器Ueditor自动换行,添加<p>的问题
- 基于UITextView的UITextField功能效果(有占位符功能)
- 【Light】[1048]Conquering Keokradong
- 第2课 GUI程序实例分析
- 【CodeChef-XRQRS】Xor Queries【可持久化Trie / +主席树】
- poj2778.DNA Sequence (Trie图 && 矩阵乘)