可可你好世界屏保

发布于 2024-08-25 14:46:39 字数 1749 浏览 7 评论 0原文

我一直在研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缺⑴份安定 2024-09-01 14:46:39
NSRect 边界 = [自身边界];
NSS大小新大小;
newSize.width =bounds.size.width;
newSize.height =bounds.size.height;
[图像设置大小:新大小];

我不知道你为什么要这样做。

NSRect imageRect;
imageRect.origin = NSZeroPoint;
imageRect.size = [图像大小];

又名[自我边界].size

NSRect 绘图矩形 = imageRect;
[图像drawInRect:drawingRect fromRect:imageRect操作:NSCompositeSourceOver分数:1];

即,[imagedrawInRect:[selfbounds]fromRect:[selfbounds]操作:NSCompositeSourceOverfraction:1]

如果您尝试以其自然大小绘制图像,则没有理由向其发送 setSize: 消息。剪掉整个第一部分,其余部分应该可以正常工作。

如果您尝试填充屏幕(这将是缩放,这与注释相矛盾),请将 drawingRect 设置为 [selfbounds],而不是 imageRect。正如它所描述的那样:

image,
    draw into (the bounds of the view),
    from (the image's entire area).

[image
    drawInRect:[self bounds]
      fromRect:imageRect
              ⋮
];

自然大小固定位置绘制和全屏绘制都不是有效的屏幕保护程序。后者是不可挽回的;您可以通过在屏幕上制作图像动画来使前者变得有用。

NSRect bounds = [self bounds];
NSSize newSize;
newSize.width = bounds.size.width;
newSize.height = bounds.size.height;
[image setSize:newSize];

I don't know why you're doing this.

NSRect imageRect;
imageRect.origin = NSZeroPoint;
imageRect.size = [image size];

A.k.a. [self bounds].size.

NSRect drawingRect = imageRect;
[image drawInRect:drawingRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1];

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], not imageRect. This does exactly as it reads:

image,
    draw into (the bounds of the view),
    from (the image's entire area).

[image
    drawInRect:[self bounds]
      fromRect:imageRect
              ⋮
];

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.

梦醒时光 2024-09-01 14:46:39

我有类似的问题,所以我将发布我的解决方案。 OP 尝试通过 NSBundle 的 mainBundle 加载图像内容。相反,您会更幸运地获取屏幕保护程序的捆绑包并从那里加载文件,如下所示:

NSBundle *saverBundle = [NSBundle bundleForClass:[self class]];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[saverBundle pathForResource:@"image" ofType:@"png"]];

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:

NSBundle *saverBundle = [NSBundle bundleForClass:[self class]];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[saverBundle pathForResource:@"image" ofType:@"png"]];
智商已欠费 2024-09-01 14:46:39

由于您正在 animateOneFrame 中进行绘图,因此请尝试删除覆盖的 drawRect

Since you are doing your drawing in animateOneFrame, try removing your overridden drawRect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文