动画许多图像时的内存问题

时间:2023-01-12 17:55:26

I have about 272 layer of images for animating my view background. Below is a sample code on how I animate the images. But doing like this I'm getting memory issue and app crashes. Any advice?

我有大约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];

2 个解决方案

#1


Using [UIImage imageNamed:] cache the image in memory, try using [UIImage imageWithContentOfFile:]. Check this answer

使用[UIImage imageNamed:]将图像缓存在内存中,尝试使用[UIImage imageWithContentOfFile:]。检查这个答案

#2


Try release object of uiimage after adding to to array:

添加到数组后尝试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];

Or try to declare uiimage outside loop and reuse it in each loop:

或者尝试在外部循环声明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];

#1


Using [UIImage imageNamed:] cache the image in memory, try using [UIImage imageWithContentOfFile:]. Check this answer

使用[UIImage imageNamed:]将图像缓存在内存中,尝试使用[UIImage imageWithContentOfFile:]。检查这个答案

#2


Try release object of uiimage after adding to to array:

添加到数组后尝试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];

Or try to declare uiimage outside loop and reuse it in each loop:

或者尝试在外部循环声明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];