【发布时间】: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 请告诉我如何减少内存使用量。
标签: ios objective-c memory-management