在不使用图像的情况下滚动滚动视图时,应用程序崩溃并在 ARC 中收到内存异常
Posted
技术标签:
【中文标题】在不使用图像的情况下滚动滚动视图时,应用程序崩溃并在 ARC 中收到内存异常【英文标题】:App crashes with received memory Exception in ARC while scrolling the scrollview with out using images 【发布时间】:2016-01-19 07:03:00 【问题描述】:我正在使用 UIScrollview
并添加 subView 来显示消息。滚动时会给出received memory exception
。我从 15 天起就很难找到这个。请帮我解决这个问题。
为了显示我第一次加载 5 条消息。滚动后根据数组计数添加子视图。如果是子视图,则根据消息长度增加。如果是长消息,则崩溃received memory exception
。
这是我的代码
-(void)createView:(float)yAxis
// NSLog(@"Create view %f",yAxis);
for (UIView * view in backgroundScroll.subviews)
if (view.tag == 15)
[view removeFromSuperview];
for(int i=0;i<[pageMessageArray count];i++)
UIView *backgrounf_View=[self messageChildElements:yAxis :i];
[del.msglistViewArray addObject:backgrounf_View];
UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i];
[backgroundScroll addSubview:backgrounf_View1];
yAxis=yAxis+oldY_Axis+65;
CGRect contentRect = CGRectZero;
for (UIView *view in backgroundScroll.subviews)
contentRect = CGRectUnion(contentRect, view.frame);
//UIView *backgrounf_View1= [del.msglistViewArray objectAtIndex:i];
backgroundScroll.contentSize = CGSizeMake(backgroundScroll.frame.size.width, yAxis-10);
lblPermenentmessage.frame=CGRectMake(30, contentRect.size.height+20,800, 50);
//LastMessageYaxis=contentRect.size.height+20;
lblPermenentmessage.text=@"Messages are displayed for 24-48 hours and automatically deleted permanently.";
-(UIView *)messageChildElements:(float)yAxis :(int)index
// NSLog(@"Create view %f",yAxis);
communityObject *objMessage=[del.messageArray objectAtIndex:index];
UIView *backgrounf_View=nil;
UITextView *txt_Message=nil;
UIButton *btnURL =nil;
UIButton *btnAttachment=nil;
UIButton *btnDate=nil;
UIButton *btnSendBy=nil;
UIButton *btnAttachmentName=nil;
UIButton *btnSendTo=nil;
NSString *labelText = objMessage.Message;
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:8];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
CGFloat height=[self getNewsize:objMessage.Message :backgroundScroll.frame.size.width-30];
backgrounf_View=[[UIView alloc]initWithFrame:CGRectMake(26,yAxis,backgroundScroll.frame.size.width-30,height)];
if([objMessage.inorout isEqualToString:@"out"])
//ye
[backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:247/255.0f blue:193/255.0f alpha:1.0f]];
[[backgrounf_View layer] setBorderWidth:1.0f];
[[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:236/255.0f green:237/255.0f blue:178/255.0f alpha:1.0f].CGColor];
else
//[cell.lblName addTarget:self action:@selector(replayClick:) forControlEvents:UIControlEventTouchUpInside];
[backgrounf_View setBackgroundColor:[UIColor colorWithRed:251/255.0f green:215/255.0f blue:245/255.0f alpha:1.0f]];
[[backgrounf_View layer] setBorderWidth:1.0f];
[[backgrounf_View layer] setBorderColor:[UIColor colorWithRed:226/255.0f green:188/255.0f blue:218/255.0f alpha:1.0f].CGColor];
// backgrounf_View.autoresizingMask=UIViewAutoresizingFlexibleWidth;
backgrounf_View.layer.cornerRadius=10;
//backgrounf_View.tag=15;
backgrounf_View.clipsToBounds=YES;
backgrounf_View.tag=objMessage.indexValue;
txt_Message = [[UITextView alloc] initWithFrame:CGRectMake(12, 6, backgroundScroll.frame.size.width-40, height)];
txt_Message.backgroundColor=[UIColor redColor];
[txt_Message setScrollEnabled:NO];
[txt_Message setUserInteractionEnabled:NO];
[txt_Message setBackgroundColor:[UIColor clearColor]];
//txt_Message.autoresizingMask=UIViewAutoresizingFlexibleWidth;
txt_Message.attributedText=attributedString;
//[txt_Message sizeToFit];
if([del.device isEqualToString:@"iphone"])
[txt_Message setFont:[UIFont fontWithName:@"Arial" size:17]];
else
[txt_Message setFont:[UIFont fontWithName:@"Arial" size:21]];
[backgrounf_View addSubview:txt_Message];
oldY_Axis=height+20;
if (objMessage.Link.length>0)
btnURL = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnURL.frame = CGRectMake(11,height,backgroundScroll.frame.size.width-100,40);
[btnURL setTitle:objMessage.Link forState:UIControlStateNormal];
btnURL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnURL.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
// btnURL.backgroundColor=[UIColor redColor];
btnURL.tag=objMessage.indexValue;
btnURL.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnURL.titleLabel.numberOfLines=2;
btnURL.titleLabel.font=[UIFont fontWithName:@"Arial" size:20];
btnURL.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnURL setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnURL];
oldY_Axis=height+btnURL.frame.size.height+20;
if (objMessage.Filename.length>0)
btnAttachment= [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAttachment.frame = CGRectMake(11, oldY_Axis,40, 40);
btnAttachment.tag=objMessage.indexValue;
UIImage *buttonattachment=[UIImage imageNamed:@"attach.png"];
[btnAttachment setBackgroundImage:buttonattachment forState:UIControlStateNormal];
btnAttachment.userInteractionEnabled=YES;
btnAttachmentName = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnAttachmentName.frame = CGRectMake(47,oldY_Axis+5,backgroundScroll.frame.size.width-150,60);
[btnAttachmentName setTitle:objMessage.Filename forState:UIControlStateNormal];
btnAttachmentName.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnAttachmentName.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnAttachmentName.tag=objMessage.indexValue;
btnAttachmentName.titleLabel.font=[UIFont fontWithName:@"Arial" size:20];
btnAttachmentName.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnAttachmentName setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnAttachment];
[backgrounf_View addSubview:btnAttachmentName];
oldY_Axis=oldY_Axis+btnAttachment.frame.size.height+30;
btnDate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnDate.frame = CGRectMake(11,oldY_Axis,130,30);
[btnDate setTitle:[NSString stringWithFormat:@"%@:",objMessage.Date] forState:UIControlStateNormal];
btnDate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnDate.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnDate.tag=objMessage.indexValue;
btnDate.titleLabel.font=[UIFont fontWithName:@"Arial" size:15];
btnDate.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnDate setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnDate];
btnSendBy = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSendBy.frame = CGRectMake(127,oldY_Axis,backgroundScroll.frame.size.width-50,200);
[btnSendBy setTitle:objMessage.senderPerson forState:UIControlStateNormal];
btnSendBy.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnSendBy.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
btnSendBy.tag=objMessage.indexValue;
// btnURL.backgroundColor=[UIColor redColor];
//btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnSendBy.titleLabel.font=[UIFont fontWithName:@"Arial" size:15];
btnSendBy.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnSendBy setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnSendBy];
btnSendTo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnSendTo.frame = CGRectMake(11,oldY_Axis+25,backgroundScroll.frame.size.width-50,200);
[btnSendTo setTitle:objMessage.senderPerson forState:UIControlStateNormal];
btnSendTo.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
btnSendTo.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
// btnURL.backgroundColor=[UIColor redColor];
//btnSendBy.titleLabel.lineBreakMode=UILineBreakModeCharacterWrap;
btnSendTo.titleLabel.font=[UIFont fontWithName:@"Arial" size:15];
btnSendTo.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
[btnSendTo setTitleColor:[UIColor colorWithRed:70/255.0f green:70/255.0f blue:70/255.0f alpha:1.0f] forState:UIControlStateNormal];
[backgrounf_View addSubview:btnSendTo];
NSString *communityName=@"";
if([del.communityId isEqualToString:@"none"])
communityName=objMessage.Community;
communityName=[NSString stringWithFormat:@"%@: ",objMessage.Community];
//NSLog(@"Group type %@ and %@",objMessage.groupType,del.communityId);
if([objMessage.groupType isEqualToString:@"Personal"])
[btnSendTo setTitle:[NSString stringWithFormat:@"%@%@",communityName,objMessage.Person] forState: UIControlStateNormal];
else if([objMessage.groupType isEqualToString:@"Everyone"])
[btnSendTo setTitle:[NSString stringWithFormat:@"%@%@",communityName,@"Everyone"] forState: UIControlStateNormal];
else
[btnSendTo setTitle:[NSString stringWithFormat:@"%@%@",communityName,objMessage.GroupName] forState: UIControlStateNormal];
[btnURL addTarget:self action:@selector(getLink:) forControlEvents:UIControlEventTouchUpInside];
[btnAttachmentName addTarget:self action:@selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside];
[btnAttachment addTarget:self action:@selector(fileDowload:) forControlEvents:UIControlEventTouchUpInside];
backgrounf_View.frame=CGRectMake(24,yAxis,backgroundScroll.frame.size.width-30,oldY_Axis+55);
LastMessageYaxis=backgrounf_View.frame.origin.y+backgrounf_View.frame.size.height+10;
del.RefreshValue=0;
LastValue=index;
communityObject *com=[[communityObject alloc]init];
com.MessageId=nil;
com.Date=nil;
com.Message=nil;
com.Link=nil;
com.Attachment=nil;
com.Filename=nil;
com.attachmentType=nil;
com.imageSize=nil;
com.groupType=nil;
com.Person=nil;
com.GroupName=nil;
com.senderPerson=nil;
com.Community=nil;
com.inorout=nil;
return backgrounf_View;
Sceenshot is added please check.
【问题讨论】:
也许你应该使用 UITableView 并从它的视图回收功能中受益。 您是否使用Instruments
- 分配/僵尸工具对您的应用进行了分析?这可能会为您提供有关应用程序内存使用情况的线索。
@jorn Buitink 我之前使用的是 UItableview,但计算高度需要时间。它不符合我的要求,所以我删除了它。我正在实现这个用户界面
@Cristik 是的,我正在使用仪器我只获得内存地址如何使用内存地址找到代码。请帮我找到。
我已添加截图请检查并帮助我
【参考方案1】:
这个位在枚举时看起来像是数组的突变:
for (UIView * view in backgroundScroll.subviews)
if (view.tag == 15)
[view removeFromSuperview];
难怪数组不喜欢它。我也会崩溃:)
顺便说一句:在视图标签中存储数组索引的那部分似乎非常错误。
【讨论】:
如果删除我也得到同样的错误。虽然快速滚动应用程序因收到内存异常而崩溃 是的。有可能的。我需要通过代码。但是在处理带有标签 15 的视图时,这部分代码肯定会崩溃。我建议将一些 NSAsserts 放在关键部分。尤其是那些使用索引访问数组的地方。找出问题所在应该不难。以上是关于在不使用图像的情况下滚动滚动视图时,应用程序崩溃并在 ARC 中收到内存异常的主要内容,如果未能解决你的问题,请参考以下文章