在我的拆分视图 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 错误。当我更改详细视图时,看起来应用程序有时会随机崩溃。我想我会创建一个新问题。
感谢所有的帮助!