In my app, i am getting an error (Message from debugger: Terminated due to memory issue) and app crashes. I have checked my app by leaks in Xcode instrument but no issue found. please help me to find a solution why this is happening?
code below shows memory warning..
code 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];
}
}
code 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];
}
code 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;
}
}
You need to release for CGImageRef and need to cover this with @autoreleasepool
I added
@autoreleasepool
and
CGImageRelease(cgImg);
cgImg = nil;
guesImg = nil;
I fixed this. Please try and share your result.
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];
}
and
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;
}
}
Please test it. I can't found any suspected code for 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.