我一直在研究 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