由于内存问题而终止的 iOS 应用程序
Posted
技术标签:
【中文标题】由于内存问题而终止的 iOS 应用程序【英文标题】:Terminated iOS app due to memory issue 【发布时间】:2017-11-08 05:46:10 【问题描述】:在我的应用程序中,我收到错误消息(来自调试器的消息:由于内存问题而终止)并且应用程序崩溃。我通过 Xcode 仪器中的泄漏检查了我的应用程序,但没有发现问题。请帮助我找到解决方案,为什么会发生这种情况?
下面的代码显示内存警告..
代码 1
if (![[[pollOptionArray objectAtIndex:[indexPath row]]valueForKey:@"option_color"] isKindOfClass:[NSNull class]])
NSArray* foo = [[[pollOptionArray objectAtIndex:[indexPath row]]valueForKey:@"option_color"] componentsSeparatedByString: @"#"];
if([[foo objectAtIndex:1]isEqualToString:@"FFFFFF"]||[[foo objectAtIndex:1]isEqualToString:@"ffffff"])
[titleLabel setBackgroundColor:[self colorWithHexString:@"000000"]];
else
[titleLabel setBackgroundColor:[self colorWithHexString:[foo objectAtIndex:1]]];
if([[foo objectAtIndex:1]isEqualToString:@"FFFF00"]||[[foo objectAtIndex:1]isEqualToString:@"ffff00"])
[titleLabel setTextColor:[self colorWithHexString:@"000000"]];
//[titleLabel setBackgroundColor:[self colorWithHexString:[foo objectAtIndex:1]]];
if(votedPoll && [votedPoll valueForKey:@"option_id"] && [[votedPoll valueForKey:@"option_id"] isEqualToString:[[pollOptionArray objectAtIndex:[indexPath row]]valueForKey:@"option_id"]])
titleLabel.shadowColor = [UIColor blueColor];
//titleLabel.layer.shadowColor = [[self colorWithHexString:[foo objectAtIndex:1]] CGColor];
titleLabel.layer.shadowRadius = 3.0f;
titleLabel.layer.shadowOpacity = 2;
titleLabel.layer.shadowOffset = CGSizeZero;
titleLabel.layer.masksToBounds = NO;
//Blur the image
UIGraphicsBeginImageContext(CGSizeMake(titleLabel.bounds.size.width, titleLabel.bounds.size.height));
[titleLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Blur the image
CIImage *blurImg = [CIImage imageWithCGImage:viewImg.CGImage];
CGAffineTransform transform = CGAffineTransformIdentity;
CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
[clampFilter setValue:blurImg forKey:@"inputImage"];
[clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];
//[gaussianBlurFilter setValue:[NSNumber numberWithFloat:0.8f] forKey:@"inputRadius"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat:2.0f] forKey:@"inputRadius"];
CIImage *guesImg=gaussianBlurFilter.outputImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImg = [context createCGImage:guesImg fromRect:CGRectMake(0,0,70,40)];
UIImage *outputImg = [UIImage imageWithCGImage:cgImg];
//Add UIImageView to current view.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x-10, titleLabel.frame.origin.y,titleLabel.frame.size.width, titleLabel.frame.size.height)];
//imgView.image = [UIImage imageNamed: @"arrow.png"];
[imgView setTag:1109];
imgView.image = outputImg;
[imgView setTag:1108];
gaussianBlurFilter = nil;
outputImg = nil;
blurImg = nil;
viewImg = nil;
[titleLabel addSubview:imgView];
UIGraphicsEndImageContext();
cell.userInteractionEnabled = NO;
else
blurImage.backgroundColor = [UIColor clearColor];
代码 2
if (![[NSString stringWithFormat:@"%@",theModal.noPercentage] isEqualToString:@""] ||[theModal.noPercentage isEqual: [NSNull null]]) self.lblNoPercentage.text = [NSString stringWithFormat:@"%@%@ %@",[NSString stringWithFormat:@"%@",theModal.noPercentage],@"%",[NSString stringWithFormat:@"%@", theModal.btn2Name] ];
if([theModal.btn2Color isEqualToString:@"FFFFFF"]||[theModal.btn2Color isEqualToString:@"ffffff"])
[self.lblNoPercentage setTextColor:[self colorWithHexString:@"000000"]];
else
[self.lblNoPercentage setTextColor:[self colorWithHexString:theModal.btn2Color]];
//[self.lblNoPercentage setTextColor:[self colorWithHexString:theModal.btn2Color]];
CGRect frame = self.btn4label.frame;
frame.origin.x= 228;
frame.origin.y= 74;
frame.size.width= 100;
[self.btn4label setFrame: frame];
代码 3
-(void)drawAnnotations
arrayIndex = 0;
NSArray *pointsArray = [self.mapVIEW overlays];
[self.mapVIEW removeOverlays:pointsArray];
for(int i = 0; i<pollListArr.count ; i++)
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
PollListHomeModal *themodal = [pollListArr objectAtIndex:i ];
annotation.coordinate = CLLocationCoordinate2DMake([themodal.poll_latitude doubleValue], [themodal.poll_longitude doubleValue]);
[self.mapVIEW addAnnotation:annotation];
annotation = nil;
【问题讨论】:
需要找出哪个代码产生了泄漏。您能否在您认为最可疑代码的地方分享您的代码。 您的应用程序可以分配比系统可以处理的更多的内存,而不会产生泄漏。您必须减少应用程序的内存使用量。 @HwanghoKim 用代码编辑了问题。 @Palle 请告诉我如何减少内存使用量。 【参考方案1】:你需要为 CGImageRef 发布并且需要用@autoreleasepool 覆盖这个
我加了
@autoreleasepool
和
CGImageRelease(cgImg);
cgImg = nil;
guesImg = nil;
我解决了这个问题。请尝试并分享您的结果。
if(votedPoll && [votedPoll valueForKey:@"option_id"] && [[votedPoll valueForKey:@"option_id"] isEqualToString:[[pollOptionArray objectAtIndex:[indexPath row]]valueForKey:@"option_id"]])
@autoreleasepool
titleLabel.shadowColor = [UIColor blueColor];
//titleLabel.layer.shadowColor = [[self colorWithHexString:[foo objectAtIndex:1]] CGColor];
titleLabel.layer.shadowRadius = 3.0f;
titleLabel.layer.shadowOpacity = 2;
titleLabel.layer.shadowOffset = CGSizeZero;
titleLabel.layer.masksToBounds = NO;
//Blur the image
UIGraphicsBeginImageContext(CGSizeMake(titleLabel.bounds.size.width, titleLabel.bounds.size.height));
[titleLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Blur the image
CIImage *blurImg = [CIImage imageWithCGImage:viewImg.CGImage];
CGAffineTransform transform = CGAffineTransformIdentity;
CIFilter *clampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
[clampFilter setValue:blurImg forKey:@"inputImage"];
[clampFilter setValue:[NSValue valueWithBytes:&transform objCType:@encode(CGAffineTransform)] forKey:@"inputTransform"];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:clampFilter.outputImage forKey: @"inputImage"];
//[gaussianBlurFilter setValue:[NSNumber numberWithFloat:0.8f] forKey:@"inputRadius"];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat:2.0f] forKey:@"inputRadius"];
CIImage *guesImg=gaussianBlurFilter.outputImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef cgImg = [context createCGImage:guesImg fromRect:CGRectMake(0,0,70,40)];
UIImage *outputImg = [UIImage imageWithCGImage:cgImg];
//Add UIImageView to current view.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x-10, titleLabel.frame.origin.y,titleLabel.frame.size.width, titleLabel.frame.size.height)];
//imgView.image = [UIImage imageNamed: @"arrow.png"];
[imgView setTag:1109];
imgView.image = outputImg;
[imgView setTag:1108];
gaussianBlurFilter = nil;
outputImg = nil;
blurImg = nil;
viewImg = nil;
[titleLabel addSubview:imgView];
UIGraphicsEndImageContext();
cell.userInteractionEnabled = NO;
CGImageRelease(cgImg);
cgImg = nil;
guesImg = nil;
else
blurImage.backgroundColor = [UIColor clearColor];
和
arrayIndex = 0;
NSArray *pointsArray = [self.mapVIEW overlays];
[self.mapVIEW removeOverlays:pointsArray];
pointsArray = nil;
for(int i = 0; i<pollListArr.count ; i++)
@autoreleasepool
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
PollListHomeModal *themodal = [pollListArr objectAtIndex:i ];
annotation.coordinate = CLLocationCoordinate2DMake([themodal.poll_latitude doubleValue], [themodal.poll_longitude doubleValue]);
[self.mapVIEW addAnnotation:annotation];
annotation = nil;
themodal = nil;
请测试一下。我找不到 2 的任何可疑代码
【讨论】:
感谢金黄浩。但是警告出现在另外 2 个代码中,请您查看一下。以上是关于由于内存问题而终止的 iOS 应用程序的主要内容,如果未能解决你的问题,请参考以下文章
“来自调试器的消息:由于内存问题而终止”的任何通知或观察者?
Xcode 11 GM-设备启动后应用程序崩溃由于信号9而终止