首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >How to- NSAttributedString to CGImageRef

How to- NSAttributedString to CGImageRef
EN

Stack Overflow用户
提问于 2010-04-28 22:49:52
回答 1查看 1.1K关注 0票数 0

我正在写一个QuickLook插件。好吧,一切都正常。我只想试一试,让它变得更好;)。因此问题就来了。

这是一个返回缩略图的函数,我现在正在使用它。

代码语言:javascript
复制
QLThumbnailRequestSetImageWithData(
QLThumbnailRequestRef thumbnail,
CFDataRef data,
CFDictionaryRef properties);
);

http://developer.apple.com/mac/library/documentation/UserExperience/Reference/QLThumbnailRequest_Ref/Reference/reference.html#//apple_ref/c/func/QLThumbnailRequestSetImageWithData

现在,我正在创建一个封装到NSData中的TIFF ->。一个例子

代码语言:javascript
复制
// Setting CFDataRef
CGSize thumbnailMaxSize = QLThumbnailRequestGetMaximumSize(thumbnail);
NSMutableAttributedString *attributedString = [[[NSMutableAttributedString alloc] 
                                                   initWithString:@"dummy" 
                                                   attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                               [NSFont fontWithName:@"Monaco" size:10], NSFontAttributeName, 
                                                               [NSColor colorWithCalibratedRed:0.0 green:0.0 blue:0.0 alpha:1.0], NSForegroundColorAttributeName, 
                                                               nil]
                                                   ] autorelease];
NSImage *thumbnailImage = [[[NSImage alloc] initWithSize:NSMakeSize(thumbnailMaxSize.width, thumbnailMaxSize.height)] autorelease];
[thumbnailImage lockFocus];
[[NSColor whiteColor] set];
NSRectFill(NSMakeRect(0, 0, thumbnailMaxSize.width, thumbnailMaxSize.height));
[attributedString drawInRect:NSMakeRect(0, 0, thumbnailMaxSize.width, thumbnailMaxSize.height)];
[thumbnailImage unlockFocus];
(CFDataRef)[thumbnailImage TIFFRepresentation]; // This is data

// Setting CFDictionaryRef
(CFDictionaryRef)[NSDictionary dictionaryWithObjectsAndKeys:@"kUTTypeTIFF", (NSString *)kCGImageSourceTypeIdentifierHint, nil ]; // this is properties

但是QuickLook提供了另一个返回缩略图的函数,即

代码语言:javascript
复制
QLThumbnailRequestSetImage(
QLThumbnailRequestRef thumbnail,
CGImageRef image,
CFDictionaryRef properties);
);

http://developer.apple.com/mac/library/documentation/UserExperience/Reference/QLThumbnailRequest_Ref/Reference/reference.html#//apple_ref/c/func/QLThumbnailRequestSetImage

我有一种感觉,将CGImage传递给QL而不是TIFF数据将有助于加快速度。然而,我以前从来没有使用过CG上下文。我知道,文档在那里:),但不管怎样-谁能给出一个例子,如何将NSAttributed字符串转换为CGImageRef。一个示例值得阅读文档10次;)

感谢您的帮助。提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-04-29 01:13:35

谁能给出一个例子,如何将NSAttributed字符串转换为CGImageRef。

您不能将字符串转换为图像;它们是两种完全不同的数据,一种是二维的(随时间变化的字符),而另一种至少是三维的(x和y上的颜色)。

您需要做的是绘制字符串并生成绘图的图像。这就是您现在使用NSImage所做的事情:创建一个图像并将字符串绘制到其中。

您正在询问有关创建CGImage的问题。创建一个bitmap context,使用Core Text将字符串绘制到其中,然后创建一个位图上下文内容的图像就是一种方法。

然而,假设您可以使用Snow Leopard,那么您已经离另一个解决方案更近了。ask it for a CGImage,而不是向NSImage请求TIFF表示。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2730527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档