NSMutableAttributedString addAttribute 方法是否保留传入的值?
例如下面的代码内存安全吗?
NSMutableAttributedString *str = ...;
CTFontRef aFont = CTFontCreateWithName((CFStringRef)fontName, size, NULL);
[str addAttribute:(NSString*)kCTFontAttributeName value:(id)aFont range:range];
CFRelease(aFont);
另外,多次调用 CTFontCreateWithName 是否有效,或者是否应该努力缓存相同字体/大小的 CTFontRef?
For example is the following code memory safe?
NSMutableAttributedString *str = ...;
CTFontRef aFont = CTFontCreateWithName((CFStringRef)fontName, size, NULL);
[str addAttribute:(NSString*)kCTFontAttributeName value:(id)aFont range:range];
CFRelease(aFont);
Also, is CTFontCreateWithName efficient to call multiple times or should some effort be made to cache CTFontRef's for the same font/size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信将字体对象添加为属性后释放它是安全的。我已经在自己的核心文本代码中这样做了,从来没有遇到任何问题。
至于缓存,如果要多次使用字体对象,则保留该字体对象比释放它并重新创建它多次更有意义。不过,这可能是预优化,所以我还不会做出任何有意识的努力。使用您当前的代码对其进行分析,并确定额外的微秒是否值得。
I believe it is safe to release the font object after adding it as an attribute. I have done so in my own Core Text code and never have any issues.
As for caching, it would make sense to keep a font object around if it will be used multiple times rather than releasing it and recreating it many times. Though, this is likely pre-optimisation, so I wouldn't make any conscious effort just yet. Profile it with your current code and decide whether or not the extra microseconds will be worth the work.