【问题标题】:App crashed due to memory pressure [closed]应用程序因内存压力而崩溃[关闭]
【发布时间】:2014-01-22 13:15:06
【问题描述】:

这里我正在生成 UIViews 的图像,为此我使用以下代码:

    // ======================== Create PDF FROM Images     ============================

- (void) drawBorder
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    UIColor *borderColor = [UIColor grayColor];

    CGRect rectFrame = CGRectMake(kBorderInset, kBorderInset, pageSize.width-kBorderInset*2, pageSize.height-kBorderInset*2);

    CGContextSetStrokeColorWithColor(currentContext, borderColor.CGColor);
    CGContextSetLineWidth(currentContext, kBorderWidth);
    CGContextStrokeRect(currentContext, rectFrame);

    //CGContextRelease(currentContext);
}

- (void)drawPageNumber:(NSInteger)pageNumber
{
    NSString* pageNumberString = [NSString stringWithFormat:@"Page %d", pageNumber];
    UIFont* theFont = [UIFont systemFontOfSize:12];

    CGSize pageNumberStringSize = [pageNumberString sizeWithFont:theFont
                                               constrainedToSize:pageSize
                                                   lineBreakMode:NSLineBreakByWordWrapping];

    CGRect stringRenderingRect = CGRectMake(kBorderInset,
                                            pageSize.height - 40.0,
                                            pageSize.width - 2*kBorderInset,
                                            pageNumberStringSize.height);

    [pageNumberString drawInRect:stringRenderingRect withFont:theFont lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
}

- (void) drawHeader:(int)i;
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    //CGContextSetRGBFillColor(currentContext, 0.3, 0.7, 0.2, 1.0);

    CGContextSetRGBFillColor(currentContext, 0.0f, 0.0f, 0.0f, 1);

    //NSString *textToDraw = [NSString stringWithFormat:@"%@: %@ \n\nDate Range: %@ \n\nNumber of observations: %@",appDelegate.teacherName,@"Curriculum report",lblDate.text,lblNoOf.text];
    NSString *textToDraw;

    if(i<[arrReportTitles count])
        textToDraw=[arrReportTitles objectAtIndex:i];

    UIFont *font = [UIFont systemFontOfSize:16.0];

    CGSize stringSize = [textToDraw sizeWithFont:font constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset) lineBreakMode:NSLineBreakByWordWrapping];

    CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset, kBorderInset + kMarginInset, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height);

    [textToDraw drawInRect:renderingRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentLeft];

    //CGContextRelease(currentContext);
}

- (void) drawText
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0);
    NSString *textToDraw = @"TeachersEvaluation";
    UIFont *font = [UIFont systemFontOfSize:24.0];
    CGSize stringSize = [textToDraw sizeWithFont:font
                               constrainedToSize:CGSizeMake(pageSize.width - 2*kBorderInset-2*kMarginInset, pageSize.height - 2*kBorderInset - 2*kMarginInset)
                                   lineBreakMode:NSLineBreakByWordWrapping];

    CGRect renderingRect = CGRectMake(kBorderInset + kMarginInset + 375, kBorderInset + kMarginInset + 450.0, pageSize.width - 2*kBorderInset - 2*kMarginInset, stringSize.height-50);
    [textToDraw drawInRect:renderingRect
                  withFont:font
             lineBreakMode:NSLineBreakByWordWrapping
                 alignment:NSTextAlignmentLeft];

    //CGContextRelease(currentContext);
}

- (void) drawLine
{
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(currentContext, kLineWidth);

    CGContextSetStrokeColorWithColor(currentContext, [UIColor grayColor].CGColor);

    CGPoint startPoint = CGPointMake(kMarginInset + kBorderInset, kMarginInset + kBorderInset + 120.0);
    CGPoint endPoint = CGPointMake(pageSize.width - 2*kMarginInset -2*kBorderInset, kMarginInset + kBorderInset + 120.0);

    CGContextBeginPath(currentContext);
    CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);

    CGContextClosePath(currentContext);
    CGContextDrawPath(currentContext, kCGPathFillStroke);

    //CGContextRelease(currentContext);
}

-(NSInteger) getImageHeighAndWidht : (NSInteger) value
{
    value = value / 2;

    if (value > 800 ) {

        [self getImageHeighAndWidht:value];

    }

    if (value<450) {

        value = value + (value/2);

    }

    return value;
}

- (void) drawImage: (UIImage *)fileName
{
    CGSize mainImageSize;
    UIImage *mainImage;

    if (fileName.size.width > 542 || fileName.size.height > 742)
    {
        if (fileName.size.height > fileName.size.width) {

            mainImageSize = CGSizeMake(470,575);
            mainImage =fileName; //[fileName scaleProportionalToSize:mainImageSize];

        } else {

            mainImageSize = CGSizeMake(290,395);
            mainImage = fileName;//[fileName scaleProportionalToSize:mainImageSize];

        }

    } else {

        mainImageSize = CGSizeMake(fileName.size.width,fileName.size.height);
        mainImage =fileName; //[fileName scaleProportionalToSize:mainImageSize];
    }

    float imageWidht;
    if (mainImage.size.width > 542)
    {
        imageWidht = 542;
    } else {
        imageWidht = mainImage.size.width;
    }

    float imageHeight;
    if (mainImage.size.height > 575)
    {
        imageHeight = 600.0;
    } else {
        imageHeight = mainImage.size.height;
    }

    //UIImage * demoImage = [UIImage imageNamed:fileName];
    [mainImage drawInRect:CGRectMake( (pageSize.width - imageWidht)/2, 150, imageWidht, imageHeight)];
}

- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

    NSInteger currentPage = 0;

//    _imgViewScreenShot.image=[arrMultiplePhots objectAtIndex:0];
//    _imgViewScreenShot2.image=[arrMultiplePhots objectAtIndex:1];


    for(int i = 0; i < [arrMultiplePhots count]; i++){

        //Start a new page.
        UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

        //Draw a page number at the bottom of each page.
        currentPage++;
        [self drawPageNumber:currentPage];

        //Draw a border for each page.
        [self drawBorder];

        //Draw text fo our header.
        [self drawHeader:i];

        //Draw a line below the header.
        [self drawLine];

        //if(i == 0) {
        //currImage = [self.view imageByRenderingView];

        //_imgViewScreenShot.image=currImage;
        [self drawImage:[arrMultiplePhots objectAtIndex:i]];
        //}
    }

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();
}

但是当我在一个循环中连续为多个视图生成图像时,我的应用程序崩溃并显示警告“应用程序因内存压力而崩溃”。

我使用工具工具找出了这次崩溃背后的原因,并在所有这些函数中都收到了内存泄漏警告。

那么如何解决上述代码中的内存泄漏问题呢?

编辑

使用自动释放后,上述函数中的内存泄漏被删除

现在我在这个函数中遇到了内存泄漏

生成图片

- (UIImage *)imageByRenderingView
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultingImage;
}

【问题讨论】:

    标签: ios objective-c xcode memory-leaks cgcontext


    【解决方案1】:

    试试这个改变,告诉我它是否有帮助:

    - (void) generatePdfWithFilePath: (NSString *)thefilePath
    {
        UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
    
        NSInteger currentPage = 0;
    
    //    _imgViewScreenShot.image=[arrMultiplePhots objectAtIndex:0];
    //    _imgViewScreenShot2.image=[arrMultiplePhots objectAtIndex:1];
    
    
        for(int i = 0; i < [arrMultiplePhots count]; i++)
        {
            @autoreleasepool {
                //Start a new page.
                UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
    
                //Draw a page number at the bottom of each page.
                currentPage++;
                [self drawPageNumber:currentPage];
    
                //Draw a border for each page.
                [self drawBorder];
    
                //Draw text fo our header.
                [self drawHeader:i];
    
                //Draw a line below the header.
                [self drawLine];
    
                //if(i == 0) {
                //currImage = [self.view imageByRenderingView];
    
                //_imgViewScreenShot.image=currImage;
                [self drawImage:[arrMultiplePhots objectAtIndex:i]];
                //}
            }
        }
    
        // Close the PDF context and write the contents out.
        UIGraphicsEndPDFContext();
    }
    

    【讨论】:

    • 您好,很抱歉回复晚了,感谢您的帮助。使用自动释放后,上述函数中的内存泄漏被删除但现在我在一个新函数中遇到了内存泄漏,我在 EDIT 标签下的问题中添加了该函数。
    • 好吧,你也应该用“@autoreleasepool”来包装对该方法的调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 2015-10-13
    • 2015-12-21
    • 2014-10-20
    • 2014-02-02
    • 2013-04-18
    相关资源
    最近更新 更多