我正在寫一個使用自定義字體(CTFontManagerRegisterFontsForURL)的IOS程序。我加載字體,將其添加爲字符串屬性,創建一個framesetter,然後創建一個框架,然後將其繪製到上下文中。 我釋放我使用的一切。儀器不會注意到泄漏,但是:內存使用增長與CTFontCreateWithName和CTFramesetterRef
使用此功能時,應用程序使用的內存會增大並且不縮小。 當我離開函數時,我的字體的保留計數是2。
下面是代碼:
CFMutableAttributedStringRef attributedStringRef = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
CFAttributedStringBeginEditing(attributedStringRef);
CFAttributedStringReplaceString(attributedStringRef, CFRangeMake(0, 0), (CFStringRef)label.text);
font = CTFontCreateWithName((CFStringRef)label.fontName, label.fontHeight, NULL);
保留字體的計數:1
CFAttributedStringSetAttribute(attributedStringRef, CFRangeMake(0, label.text.length), kCTFontAttributeName, font);
CFAttributedStringEndEditing(attributedStringRef);
保留字體的計數:2
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, rect);
CFRelease(font);
保留字體的計數: 1
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString(attributedStringRef);
保留字體的計數:3
CFRelease(attributedStringRef);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter,
CFRangeMake(0, 0),
path, NULL);
保留字體的計數:5
CFRelease(frameSetter);
保留字體的計數:4
CTFrameDraw(frame, ctx);
CFRelease(frame);
保留字體的計數:2
CGPathRelease(path);
有沒有某種緩存?我真的需要立即刷新該字體使用的內存。
P.S:我用CFGetRetainCount來獲取字體的保留數。
謝謝!
我使用Memory Allocation工具和Memory Leaks工具在Instruments中運行它。我沒有得到任何內存泄漏,但是使用分配工具,我可以看到我的字體仍然被分配(並且我的應用程序內存使用率更高)。我的猜測是有些東西仍然保留着它,但是什麼?爲什麼?以及我如何強制釋放?你的代碼中的 – Ben
我會說'ctx'正在保留它以供個人使用。你在內存中只有一個,或者當你運行你的代碼時,它們正在堆積起來? –
ctx如果以這種方式獲得:UIGraphicsBeginImageContext,然後UIGraphicsGetCurrentContext,然後我調用我的方法,然後我UIGraphicsGetImageFromCurrentImageContext和UIGraphicsEndImageContext。 – Ben