1

私は 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
4

3 に答える 3

3
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];

別名[self bounds].size

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

すなわち、[image drawInRect:[self bounds] fromRect:[self bounds] operation:NSCompositeSourceOver fraction:1]

setSize:画像を自然なサイズで描画しようとしている場合、メッセージを送信する理由はありません。その最初の部分全体を切り取ると、残りは問題なく機能するはずです.

画面をいっぱいにしようとしている場合 (これはスケーリングであり、コメントと矛盾します)、drawingRect[self bounds]ではなくに設定しimageRectます。これは、次のとおりです。

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

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

自然なサイズの固定位置描画もフルスクリーン描画も効果的なスクリーン セーバーではありません。後者は取り返しのつかないものです。画面の周りの画像をアニメーション化することで、前者を便利にすることができます。

于 2010-03-17T07:38:37.717 に答える
3

同様の問題がありましたので、私の解決策を投稿します。OP は、NSBundle の mainBundle を介して画像コンテンツをロードしようとしていました。代わりに、次のように、スクリーンセーバーのバンドルをフェッチしてそこからファイルをロードすると、より運が良くなります。

NSBundle *saverBundle = [NSBundle bundleForClass:[self class]];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[saverBundle pathForResource:@"image" ofType:@"png"]];
于 2012-05-13T08:22:06.930 に答える
0

で描画を行っているためanimateOneFrame、オーバーライドされた を削除してみてくださいdrawRect

于 2012-05-07T06:39:07.860 に答える