1
메모리 압력에 추락 :앱으로 인해 내가 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;
}
늦어서 죄송합니다. 도움을 주셔서 감사합니다. autorelease 메모리 누수를 사용하면 위의 함수에서 제거됩니다. 이제 EDIT 태그 아래에 제 질문에 추가 한 새로운 함수에서 메모리 누수가 발생합니다. – ViruMax
음, 그 메소드에 대한 호출을 "@autoreleasepool"과 함께 포장해야합니다. – arturgrigor