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