收到内存警告iOS

时间:2013-08-05 17:32:50

标签: ios didreceivememorywarning

在我的拆分视图iPad应用程序中,默认详细信息视图从数组加载随机图像,并在用户返回该视图时执行此操作。应用程序加载该视图正常,我可以转到另一个视图。问题是如果我回到那个视图,有时它会崩溃,如果我在返回默认视图后选择另一个视图,它有时会崩溃。我运行泄漏工具时没有显示任何泄漏,每次发生崩溃时我都不会在日志中显示任何内容。我确实收到了一个“收到的内存警告”日志,因此崩溃必须与某处的泄漏有关,我只是不确定在哪里。我正在使用ARC。有什么想法吗?

这是我的viewDidLoad方法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIImage *agelity = [UIImage imageNamed:@"Agelity"];
    UIImage *agelity2 = [UIImage imageNamed:@"Agelity2"];
    UIImage *biltmore = [UIImage imageNamed:@"Biltmore"];
    UIImage *biltmore2 = [UIImage imageNamed:@"Biltmore2"];
    UIImage *biltmore3 = [UIImage imageNamed:@"Biltmore3"];
    UIImage *choice = [UIImage imageNamed:@"Choice"];
    UIImage *enterprise = [UIImage imageNamed:@"Enterprise"];
    UIImage *enterprise2 = [UIImage imageNamed:@"Enterprise2"];
    UIImage *grainger = [UIImage imageNamed:@"Grainger"];
    UIImage *grainger2 = [UIImage imageNamed:@"Grainger2"];
    UIImage *greatWolf = [UIImage imageNamed:@"Great_Wolf"];
    UIImage *greatWolf2 = [UIImage imageNamed:@"Great_Wolf2"];
    UIImage *officeDepot = [UIImage imageNamed:@"Office_Depot1"];
    UIImage *officeDepot2 = [UIImage imageNamed:@"Office_Depot2"];
    UIImage *officeDepot3 = [UIImage imageNamed:@"Office_Depot3"];
    UIImage *sams = [UIImage imageNamed:@"Sams"];
    UIImage *sams2 = [UIImage imageNamed:@"Sams2"];

    NSMutableArray *benefitAds = [[NSMutableArray alloc]initWithObjects:agelity, agelity2, biltmore, biltmore2, biltmore3, choice, enterprise, enterprise2, grainger, grainger2, greatWolf, greatWolf2, officeDepot, officeDepot2, officeDepot3, sams, sams2, nil];

    int randomIndex = arc4random() % [benefitAds count];

    adImage.image = [benefitAds objectAtIndex:randomIndex];

    [self configureView];
}

编辑:我正在尝试使用imageWithData而不是imageNamed的建议,所以我这样做:

NSData *agelityData = [NSData dataWithContentsOfFile:@"Agelity"];
    UIImage *agelity = [UIImage imageWithData:agelityData];

但现在应用程序在启动时崩溃了:

int randomIndex = arc4random() % [benefitAds count];

使用:

Thread 1: EXC_ARITHMETIC(code=EXC_I386_DIV, subcode=0x0)

当我在我的设备而不是模拟器上运行它时,我得到了这个:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3051310543 beyond bounds for empty array'

编辑:我设置了一个异常断点,因为我收到了一个exc_bad_access code = 1错误。当我正在更改详细视图时,看起来应用程序会随机崩溃。我想我会创建一个新问题。

感谢您的帮助!

3 个答案:

答案 0 :(得分:5)

我不知道是不是导致崩溃的原因(可能性很高),但我真的建议你不要将所有图像存储在阵列中。

更好的方法是存储图像的名称,并且只选择一个UIImage,并选择名称。

见:

- (void)viewDidLoad
{ 
    NSMutableArray *benefitAds = [[NSMutableArray alloc]initWithObjects:@"Agelity", @"Agelity2", @"Biltmore", @"Biltmore2", @"Biltmore3", @"Choice", @"Enterprise", @"Enterprise2", @"Grainger", @"Grainger2", @"Great_Wolf", @"Great_Wolf2", @"Office_Depot1", @"Office_Depot2", @"Office_Depot3", @"Sams", @"Sams2", nil];

   int randomIndex = arc4random() % [benefitAds count];

   if(randomIndex < [benefitAds count]) {
       adImage.image = [UIImage imageNamed:[benefitAds objectAtIndex:randomIndex]];
       [self configureView];
   }
   else
   {
     //error message
   }   
}

请提供一些有效的反馈意见。

编辑:

在使用之前,尝试检查随机数get是否真的是一个有效的索引。

答案 1 :(得分:3)

imageNamed:使用内部缓存,它不会在内存警告上自行清空。试试imageWithData

答案 2 :(得分:1)

- (void)didReceiveMemoryWarning {

    if([self isViewLoaded] && self.view.window == nil) {
       self.view = nil;
    }

    [super didReceiveMemoryWarning];
}