gpt4 book ai didi

ios - 由于 CTFont Ref 导致的内存泄漏

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:18:34 24 4
gpt4 key购买 nike

我正在使用以下代码生成 PDF,但它会导致内存泄漏,有人可以帮忙吗?代码如下。

- (void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect {


NSMutableAttributedString *string = [[[NSMutableAttributedString alloc]
initWithString:textToDraw] autorelease];

// make a few words bold

CTFontRef helveticaBold = CTFontCreateWithName(CFSTR("Helvetica-Bold"), 8.0, NULL);

[string addAttribute:(id)kCTFontAttributeName
value:(id)helveticaBold
range:NSMakeRange(0, [string length])];

// add some color.
if (_flag == 1) {

[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor whiteColor].CGColor
range:NSMakeRange(0, [string length])];


} else {

[string addAttribute:(id)kCTForegroundColorAttributeName
value:(id)[UIColor blackColor].CGColor
range:NSMakeRange(0, [string length])];
}

// layout master
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)string);

CGMutablePathRef framePath = CGPathCreateMutable();
CGPathAddRect(framePath, NULL, frameRect);

// Get the frame that will do the rendering.
CFRange currentRange = CFRangeMake(0, 0);
CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
CGPathRelease(framePath);

// Get the graphics context.
CGContextRef currentContext = UIGraphicsGetCurrentContext();

// Put the text matrix into a known state. This ensures
// that no old scaling factors are left in place.
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
CGContextSetRGBFillColor(currentContext, 0, 0, 0, 1.0);

// Core Text draws from the bottom-left corner up, so flip
// the current transform prior to drawing.
CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
CGContextScaleCTM(currentContext, 1.0, -1.0);

// Draw the frame.
CTFrameDraw(frameRef, currentContext);

CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);

CFRelease(frameRef);
//CFRelease(stringRef);
CFRelease(framesetter);

}

我在生成时多次调用这个函数

PDF 每次都会导致内存泄漏。

最佳答案

CTFontCreateWithName 遵循 create-name-rule,即如果你创建它,你就拥有它,并且你必须在完成后释放它:

CFRelease(helveticaBold);

关于ios - 由于 CTFont Ref 导致的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14687568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com