我有一个带有集合视图的应用程序,此集合视图中的一个单元格重定向到外部链接。
每当该链接打开时,应用程序在后台崩溃并提供调试器:
“由于内存问题而终止”。
如果我只按主页按钮,应用程序会继续正常工作。
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) Portrait = NO;
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) Portrait = YES;
else Portrait = [self getStatusBarOrientations];
if(indexPath.row == 4 && indexPath.section == 0)
{
NewsListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell4" forIndexPath:indexPath];
if ([languageID isEqualToString:@"1025"])
{
cell.title.text =[NSString stringWithFormat:@"جريدة اليوم %@", [self.pdfCoverDict valueForKey:@"PdfDate"]];
[cell.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
} else {
cell.title.text =[NSString stringWithFormat:@"Today's Edition %@", [self.pdfCoverDict valueForKey:@"PdfDate"]];
}
NSURL *imageUrl = [NSURL URLWithString:[self.pdfCoverDict valueForKey:@"CoverImage"]];
[cell.imageView sd_setImageWithURL:imageUrl];
cell.imageViewWidth.constant = Portrait ? 246 : 331;
cell.imageViewHeight.constant = Portrait ? 350 : 471;
return cell;
} else if(indexPath.row == 0) {
NewsListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
cell.title.text = [self.channelKeys objectAtIndex:indexPath.section];
cell.backView.hidden = [channelID isEqualToString:@"1"] ? (indexPath.section == 0 ? YES : NO) : YES;
if([languageID isEqualToString:@"1025"]) {
[cell.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
} else {
[cell.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
[cell.title setTransform:CGAffineTransformMakeScale(-1, 1)];
}
return cell;
} else {
NSInteger row;
if(indexPath.row != 1 && indexPath.row != 2 && indexPath.row != 3) {
row = indexPath.row - 2;
} else {
row = indexPath.row - 1;
}
NSMutableArray * articles = [self.channelArticles valueForKey:[self.channelKeys objectAtIndex:indexPath.section]];
// if(row < articles.count) {
NSString *articleImage = [[articles objectAtIndex:row] valueForKey:@"ArticleMedia"];
NSString *cellIdentifier;
NSString *articleLangID = [NSString stringWithFormat:@"%@", [[articles objectAtIndex:row] valueForKey:@"LanguageID"]];
if(indexPath.section == 0) {
switch (indexPath.row)
{
case 1:
if(![articleImage isEqualToString:@""]) cellIdentifier = @"Cell1";
else cellIdentifier = @"CellNoImg";
break;
case 2:
if(![articleImage isEqualToString:@""]) cellIdentifier = @"Cell2";
else cellIdentifier = @"CellNoImg";
break;
case 3:
if(![articleImage isEqualToString:@""]) cellIdentifier = @"Cell3";
else cellIdentifier = @"CellNoImg";
break;
case 5:
if(![articleImage isEqualToString:@""]) cellIdentifier = @"Cell5";
else cellIdentifier = @"CellNoImg";
break;
case 6:
if(![articleImage isEqualToString:@""]) cellIdentifier = @"Cell5";
else cellIdentifier = @"CellNoImg";
break;
default:
if(![articleImage isEqualToString:@""]) {
if([channelID isEqualToString:@"1"]) cellIdentifier = @"Cell6";
else cellIdentifier = @"Cell5";
} else {
cellIdentifier = @"CellNoImg";
}
break;
}
} else {
if(![articleImage isEqualToString:@""]) cellIdentifier = @"Cell5";
else cellIdentifier = @"CellNoImg";
}
NewsListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if([[[articles objectAtIndex:row] valueForKey:@"LabelName"] isEqualToString:@""]) {
cell.labelNameHeight.constant = 0;
} else {
cell.labelNameHeight.constant = 21;
}
cell.labelName.text = [[articles objectAtIndex:row] valueForKey:@"LabelName"];
cell.title.text = [[articles objectAtIndex:row] valueForKey:@"MainHeadline"];
if(indexPath.row == 3 && indexPath.section == 0) {
if(![articleImage isEqualToString:@""]) cell.titleHeight.constant = [self lineCountForLabel:cell.title labelWidth:cell.frame.size.width - Portrait ? 254 : 337] * 35;
else cell.titleHeight.constant = [self lineCountForLabel:cell.title labelWidth:cell.frame.size.width - 10] * 35;
} else {
cell.titleHeight.constant = [self lineCountForLabel:cell.title labelWidth:cell.frame.size.width - 10] * 35;
}
NSString *authorTime = [self getAuthorTime:[articles objectAtIndex:row]];
if([authorTime isEqualToString:@""]) {
cell.authorTimeHeight.constant = 0;
} else {
cell.authorTimeHeight.constant = 21;
}
cell.authorTime.text = authorTime;
NSString *details = [[articles objectAtIndex:row] valueForKey:@"Introduction"];
cell.details.attributedText = [self getAttributedString: details language: articleLangID];
if([cellIdentifier isEqualToString:@"CellNoImg"]) {
defaultLines = 5;
if([cell.authorTime.text isEqualToString:@""]) defaultLines++;
if([cell.labelName.text isEqualToString:@""]) defaultLines++;
if(cell.titleHeight.constant / 35 == 1) defaultLines++;
cell.details.numberOfLines = defaultLines;
}
if(indexPath.section == 0) {
switch (indexPath.row)
{
case 2:
cell.imageViewWidth.constant = Portrait ? 240 : 323;
cell.imageViewHeight.constant = Portrait ? 151 : 199;
break;
case 3:
cell.imageViewWidth.constant = Portrait ? 240 : 323;
cell.imageViewHeight.constant = Portrait ? 151 : 199;
case 5:
cell.imageViewWidth.constant = Portrait ? 240 : 323;
cell.imageViewHeight.constant = Portrait ? 151 : 199;
break;
case 6:
cell.imageViewWidth.constant = Portrait ? 240 : 323;
cell.imageViewHeight.constant = Portrait ? 151 : 199;
break;
default:
cell.imageViewWidth.constant = Portrait ? 240 : 323;
cell.imageViewHeight.constant = Portrait ? 151 : 199;
break;
}
} else {
cell.imageViewWidth.constant = Portrait ? 240 : 323;
cell.imageViewHeight.constant = Portrait ? 151 : 199;
}
if (![articleImage isEqualToString:@""]) {
NSURL *imageUrl = [NSURL URLWithString:articleImage];
[cell.imageView sd_setImageWithURL:imageUrl];
} else {
[cell.imageView sd_setImageWithURL:nil];
}
if([articleLangID isEqualToString:@"1025"]) {
[cell.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
cell.labelName.textAlignment = NSTextAlignmentRight;
cell.title.textAlignment = NSTextAlignmentRight;
cell.authorTime.textAlignment = NSTextAlignmentRight;
cell.details.textAlignment = NSTextAlignmentRight;
} else {
if (indexPath.row == 3) {
[cell.contentView setTransform:CGAffineTransformMakeScale(-1, 1)];
[cell.labelName setTransform:CGAffineTransformMakeScale(-1, 1)];
[cell.title setTransform:CGAffineTransformMakeScale(-1, 1)];
[cell.authorTime setTransform:CGAffineTransformMakeScale(-1, 1)];
[cell.details setTransform:CGAffineTransformMakeScale(-1, 1)];
[cell.imageView setTransform:CGAffineTransformMakeScale(-1, 1)];
}
cell.labelName.textAlignment = NSTextAlignmentLeft;
cell.title.textAlignment = NSTextAlignmentLeft;
cell.authorTime.textAlignment = NSTextAlignmentLeft;
cell.details.textAlignment = NSTextAlignmentLeft;
}
return cell;
}
}
答案 0 :(得分:7)
这是怎么回事?一旦你的应用程序在后台,你就可以随时杀死它 - 它不必使用过多的内存。
如果新的前台应用需要更多内存,操作系统会尽可能多地提供内存。它会向任何后台应用程序发送内存警告。如果他们没有通过减少内存占用而做出响应,或者前台应用程序仍然需要更多内存,则后台应用程序将被终止。
你的应用程序没有崩溃,它被操作系统杀死,因为它不再是前台应用程序,而且其他地方需要其资源。
答案 1 :(得分:0)
我有同样的问题 - 当内存使用量仍然很低时因内存问题而终止。
然后我找到了原因:实际上分配的内存非常快。
仅仅因为内存被很快破坏,所以内存使用量似乎仍然非常低。 您可以使用仪器查看当前内存使用情况/总内存使用情况 picture to explain how view memory usage / total memory usage 图片来自互联网,但清楚地解释了问题。 你会发现这个值在快速减少,浅色区域快速增长,然后当内存使用率仍然很低时你的应用程序因内存问题而崩溃。
答案 2 :(得分:0)
我会检查您代码中的其他地方。答案很可能是您的某个位置存在保留周期并正在泄漏内存。搜索诸如闭包之类的项目,其中您可能声明自己为弱状态(可能为零)或不属于自身(在这种情况下绝不为零,但闭包对象不拥有该自我)
[weak self]
或
[unowned self]
仪器可以为您提供帮助,但是需要对使用中的对象(控制器,自定义视图类,自定义tableviewcell子类,可能引用自身的tableviewrowaction委托方法等)进行详细检查。找到可疑区域并进行更正后,应测试设备的行为并查看是否可以发现仪器中的泄漏。
请确认您的修复程序消除了泄漏,进行了更多测试,并确认没有其他泄漏发生(很可能是一个以上的泄漏)。
答案 3 :(得分:0)
如果仅在应用程序不活动时才出现此问题,则也可能是由测试设备上的其他(内存不足)应用程序引起的。要排除此情况,请禁用其他应用程序的“后台应用程序刷新”和/或关闭其他应用程序和/或重新启动设备。