0

我正在写一个 QuickLook 插件。好吧,一切正常。只是想尝试让它变得更好;)。因此问题。
这是一个返回缩略图图像的函数,我现在正在使用它。

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

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

// 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 提供了另一个函数来返回缩略图,即

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 次 ;)
任何帮助表示赞赏。提前致谢!

4

1 回答 1

0

谁能举例说明如何将 NSAttributed 字符串转换为 CGImageRef。

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

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

你问的是创建一个 CGImage。创建位图上下文,使用Core Text将字符串绘制到其中,并创建位图上下文内容的图像是一种方法。

但是,假设您需要 Snow Leopard,您已经更接近于另一个解决方案。与其向 NSImage 询问 TIFF 表示,不如向它询问 CGImage

于 2010-04-28T17:13:34.920 回答