可可你好世界屏保
我一直在研究 NSView,因此我想尝试一下屏幕保护程序。我已经能够在 NSView 中显示和图像,但我无法修改此示例代码以在 ScreenSaverView 中显示简单的图片。
http://www.mactech.com/articles/mactech/Vol .20/20.06/ScreenSaversInCocoa/
顺便说一句,这是一个与 Snow Leopard 一起使用的很棒的教程。
我想简单地显示一个图像,我需要看起来像这样的东西......
我做错了什么?
//
// try_screensaverView.m
// try screensaver
//
#import "try_screensaverView.h"
@implementation try_screensaverView
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
[self setAnimationTimeInterval:1]; //refresh once per sec
}
return self;
}
- (void)startAnimation
{
[super startAnimation];
NSString *path = [[NSBundle mainBundle] pathForResource:@"leaf" ofType:@"JPG" inDirectory:@""];
image = [[NSImage alloc] initWithContentsOfFile:path];
}
- (void)stopAnimation
{
[super stopAnimation];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
}
- (void)animateOneFrame
{
//////////////////////////////////////////////////////////
//load image and display This does not scale the image
NSRect bounds = [self bounds];
NSSize newSize;
newSize.width = bounds.size.width;
newSize.height = bounds.size.height;
[image setSize:newSize];
NSRect imageRect;
imageRect.origin = NSZeroPoint;
imageRect.size = [image size];
NSRect drawingRect = imageRect;
[image drawInRect:drawingRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1];
}
- (BOOL)hasConfigureSheet
{
return NO;
}
- (NSWindow*)configureSheet
{
return nil;
}
@end
I have been studying NSView and as such I thought I would give a shot at a screen saver. I have been able to display and image in an NSView but I can't seen to modify this example code to display a simple picture in ScreenSaverView.
http://www.mactech.com/articles/mactech/Vol.20/20.06/ScreenSaversInCocoa/
BTW great tutorial that works with Snow Leopard.
I would think to simply display an image I would need something that looked like this...
What am I doing wrong?
//
// try_screensaverView.m
// try screensaver
//
#import "try_screensaverView.h"
@implementation try_screensaverView
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
[self setAnimationTimeInterval:1]; //refresh once per sec
}
return self;
}
- (void)startAnimation
{
[super startAnimation];
NSString *path = [[NSBundle mainBundle] pathForResource:@"leaf" ofType:@"JPG" inDirectory:@""];
image = [[NSImage alloc] initWithContentsOfFile:path];
}
- (void)stopAnimation
{
[super stopAnimation];
}
- (void)drawRect:(NSRect)rect
{
[super drawRect:rect];
}
- (void)animateOneFrame
{
//////////////////////////////////////////////////////////
//load image and display This does not scale the image
NSRect bounds = [self bounds];
NSSize newSize;
newSize.width = bounds.size.width;
newSize.height = bounds.size.height;
[image setSize:newSize];
NSRect imageRect;
imageRect.origin = NSZeroPoint;
imageRect.size = [image size];
NSRect drawingRect = imageRect;
[image drawInRect:drawingRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1];
}
- (BOOL)hasConfigureSheet
{
return NO;
}
- (NSWindow*)configureSheet
{
return nil;
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道你为什么要这样做。
又名
[自我边界].size
。即,
[imagedrawInRect:[selfbounds]fromRect:[selfbounds]操作:NSCompositeSourceOverfraction:1]
。如果您尝试以其自然大小绘制图像,则没有理由向其发送
setSize:
消息。剪掉整个第一部分,其余部分应该可以正常工作。如果您尝试填充屏幕(这将是缩放,这与注释相矛盾),请将
drawingRect
设置为[selfbounds]
,而不是imageRect
。正如它所描述的那样:自然大小固定位置绘制和全屏绘制都不是有效的屏幕保护程序。后者是不可挽回的;您可以通过在屏幕上制作图像动画来使前者变得有用。
I don't know why you're doing this.
A.k.a.
[self bounds].size
.i.e.,
[image drawInRect:[self bounds] fromRect:[self bounds] operation:NSCompositeSourceOver fraction:1]
.If you're trying to draw the image at its natural size, there's no reason to ever send it a
setSize:
message. Cut out that entire first part, and the rest should work just fine.If you're trying to fill the screen (which would be scaling, which would contradict the comment), set the
drawingRect
to[self bounds]
, notimageRect
. This does exactly as it reads:Neither the natural-size-fixed-position draw nor the full-screen draw is an effective screen saver. The latter is irredeemable; you can make the former useful by animating the image around the screen.
我有类似的问题,所以我将发布我的解决方案。 OP 尝试通过 NSBundle 的 mainBundle 加载图像内容。相反,您会更幸运地获取屏幕保护程序的捆绑包并从那里加载文件,如下所示:
I had a similar issue, so I'll post my solution. OP was attempting to load image contents via NSBundle's mainBundle. Instead, you'll have more luck fetching the screensaver's bundle and loading files from there, like so:
由于您正在
animateOneFrame
中进行绘图,因此请尝试删除覆盖的drawRect
。Since you are doing your drawing in
animateOneFrame
, try removing your overriddendrawRect
.