2010-03-17 6 views
1

나는 NSView를 연구 중이므로 스크린 세이버에서 촬영할 것이라고 생각했습니다. NSView에서 이미지를 표시하고 이미지를 렌더링 할 수 있었지만이 예제 코드를 수정하여 ScreenSaverView에 간단한 그림을 표시 할 수는 없습니다. 스노우 레오파드 (Snow Leopard)와 함께 작동코코아 hello world screensaver

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 
+1

대신에 무엇을할까요? (BTW, 당신이 이미지를 유출하고있어.) (non-GC 용) 객체를 놓고 누설을 고치기 위해 ivar을 nil (GC 용)으로 설정하십시오. –

답변

2
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: 메시지를 보내는 이유가 없습니다. 전체 첫 부분을 잘라내어 나머지는 잘 작동합니다. 당신이 (주석 모순되는 것 확장 될 것이다,) 화면을 채우기 위해 노력하는 경우

[self bounds]하지 imageRectdrawingRect을 설정합니다.

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

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

어느 자연 크기의 고정 위치 무승부를하거나 전체 화면 무승부 효과적인 화면 보호기입니다 : 그것은 읽는이 정확히 않습니다. 후자는 상환 할 수 없다. 이미지를 화면 주위에 움직이게하여 전자를 유용하게 만들 수 있습니다.

+0

이것은 좋은 일이지만 여전히 작동하지 않습니다. 시작 애니메이션에서 이미지를로드해야합니까? 나에게 일하는 .m 파일을 친절하게 보내시겠습니까? 저녁 간단 .m 파일 미리 감사드립니다! –

+0

아마 이전에 이미지를로드해야합니다. 나는 아마'initWithBundle :'에서 그것을 할 것이다. 그렇게 한 후에도 여전히 작동하지 않으면 "작동하지 않음"을 정의해야합니다. –

0

animateOneFrame에서 드로잉을 수행 중이므로 오버라이드 된 drawRect을 제거하십시오.

2

나는 비슷한 문제가있어서 해결책을 게시 할 것입니다. OP가 NSBundle의 mainBundle을 통해 이미지 내용을로드하려고 시도했습니다. 대신 화면 보호기의 번들을 가져오고 거기에서 파일을로드하는 데 더 많은 행운이 있습니다.

NSBundle *saverBundle = [NSBundle bundleForClass:[self class]]; 
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[saverBundle pathForResource:@"image" ofType:@"png"]];