views:

189

answers:

1

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
+1  A: 
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.

Peter Hosey
This is good, but it still doesn't work. Should I load the image in start animation?Would you be so kind as to send me a working .m file? Supper simple .m filethanks in advance!
RW
You should probably load the image earlier than that; I would probably do it in `initWithBundle:`. If it still doesn't work after you do that, then you're going to need to define “doesn't work”.
Peter Hosey