由于内存问题终止iOS应用

时间:2017-11-08 05:46:11

标签: ios objective-c memory-management

在我的应用程序中,我收到错误(来自调试器的消息:由于内存问题而终止)和应用程序崩溃。我已经通过Xcode仪器中的泄漏检查了我的应用程序,但没有发现问题。请帮我找到解决方案,为什么会这样?

enter image description here

下面的

代码显示内存警告..

代码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

- (无效)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;
}

}

1 个答案:

答案 0 :(得分:0)

你需要发布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