我有大约272层图像用于动画我的视图背景。下面是关于如何为图像设置动画的示例代码。但这样做我得到内存问题和应用程序崩溃。有什么建议吗?
NSMutableArray *imagesArray = [NSMutableArray array];
for (int i = 272; i >=1; i--)
{
NSString *imageName = [NSString stringWithFormat:@"layer-%d.jpg", i];
UIImage *bgImage =[UIImage imageNamed:imageName];
[imagesArray addObject:bgImage];
}
[self.bgImageView setAnimationDuration:7.0f];
[self.bgImageView setAnimationImages:imagesArray];
[self.bgImageView startAnimating];
答案 0 :(得分:1)
使用[UIImage imageNamed:]将图像缓存在内存中,尝试使用[UIImage imageWithContentOfFile:]。请检查此answer
答案 1 :(得分:0)
添加到数组后尝试使用uiimage的释放对象:
NSMutableArray *imagesArray = [NSMutableArray array];
for (int i = 272; i >=1; i--)
{
NSString *imageName = [NSString stringWithFormat:@"layer-%d.jpg", i];
UIImage *bgImage =[UIImage imageNamed:imageName];
[imagesArray addObject:bgImage];
bgImage=nil;
imageName=nil;
}
[self.bgImageView setAnimationDuration:7.0f];
[self.bgImageView setAnimationImages:imagesArray];
[self.bgImageView startAnimating];
或尝试在外循环中声明uiimage并在每个循环中重用它:
NSMutableArray *imagesArray = [NSMutableArray array];
UIImage *bgImage;
NSString *imageName;
for (int i = 272; i >=1; i--)
{
imageName = [NSString stringWithFormat:@"layer-%d.jpg", i];
bgImage =[UIImage imageNamed:imageName];
[imagesArray addObject:bgImage];
}
[self.bgImageView setAnimationDuration:7.0f];
[self.bgImageView setAnimationImages:imagesArray];
[self.bgImageView startAnimating];