为什么 bitmapImageRepForCachingDisplayInRect: 创建一个空图像?
我有一段非常简单的代码,应该捕获视图的位图。这曾经在 Leopard 中有效,但在 Snow Leopard 中似乎严重损坏。
以下是响应窗口上的按钮按下的代码:
- (IBAction)snapshot:(id)sender
{
NSView* view = [[sender window] contentView];
NSBitmapImageRep* bitmap
= [view bitmapImageRepForCachingDisplayInRect:[view bounds]];
NSData *tiff = [bitmap TIFFRepresentation];
[tiff writeToFile:[@"~/Desktop/snapshot.tiff" stringByExpandingTildeInPath]
atomically:YES];
}
单击按钮拍摄快照只会产生完全透明的图像。
我在这里完全一无所知,还是这个位图缓存方法被破坏了?
一个简单的项目 - 基本上是一个入门 NSDocument 项目,带有一个调用此代码的按钮 - 可以找到 此处。
I have a very simple bit of code that is supposed to capture the bitmap of a view. This used to work in Leopard, but seems horribly broken in Snow Leopard.
Here is the code, responding to a button press on the window:
- (IBAction)snapshot:(id)sender
{
NSView* view = [[sender window] contentView];
NSBitmapImageRep* bitmap
= [view bitmapImageRepForCachingDisplayInRect:[view bounds]];
NSData *tiff = [bitmap TIFFRepresentation];
[tiff writeToFile:[@"~/Desktop/snapshot.tiff" stringByExpandingTildeInPath]
atomically:YES];
}
Clicking on the button to take a snapshot just results in a fully transparent image.
Am I just completely clueless here, or is this bitmap caching method broken?
A simple project — basically a starter NSDocument project, with a button that calls this code -- can be found here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-bitmapImageRepForCachingDisplayInRect:
实际上并没有捕获任何内容;它只是生成一个准备缓存的空白位图。您需要调用 -cacheDisplayInRect:toBitmapImageRep: 来执行此操作。-bitmapImageRepForCachingDisplayInRect:
doesn't actually capture anything; it just generates a blank bitmap ready for caching. You need to call-cacheDisplayInRect:toBitmapImageRep:
to do that.