我正在写一个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
现在,我正在创建一个封装到NSData中的TIFF ->。一个例子
// 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次;)
感谢您的帮助。提前感谢!
发布于 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表示。
https://stackoverflow.com/questions/2730527
复制相似问题