我一直在研究NSView,因此我想我会给屏幕保护程序一个镜头。我已经能够在NSView中显示和图像,但我无法看到修改此示例代码以在ScreenSaverView中显示简单的图片。可可你好世界屏保
http://www.mactech.com/articles/mactech/Vol.20/20.06/ScreenSaversInCocoa/
BTW伟大的教程,与雪豹的工作。
我想简单地显示图像,我需要的东西,看起来像这样...
我在做什么错?
//
// 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
它代替什么? (顺便说一下,你正在泄漏图像,释放物体(对于非GC),并将伊娃设置为零(用于GC)来修复泄漏。) – 2010-03-17 07:24:13