2009-08-10 7 views
4

나는 간단한 Obj C 프로그램을 사용하여 이미지를로드하고 이론적으로 그 이미지를 확대하고 회전 할 수있게해야합니다. NSAffineTranslations를 사용하고 있습니다.NSView에서 이미지 그리기

왼쪽 위의 PS/PDF 표준과 반대로 이미지가 왼쪽 상단에 잠겨 있으므로 isFlipped를 사용하고 [afTrans scaleXBy : 1.0 yBy : -1.0]을 호출합니다.

문제는 어떤 이유로 든 내 drawRect가 처음 호출 된 후 변형이 발생하지 않는다는 것입니다.

나는 이미지를로드

, 그것은 등장하고, 올바른 보인다. 창의 크기 (drawRect를 호출하는)를 변경하면 이미지가 그려 지지만 거꾸로 뒤집혀 반전됩니다. 이것은 변형이 효과가 없다는 것을 의미합니다. 두 번째 시간에는 어떤 데이터에도 차이가 보이지 않습니다.

- (void)drawRect:(NSRect)rect 
{ 
    // Drawing code here. 

// NSLog(@"window type: %d", [[self window] backingType]); 
    NSAffineTransform *afTrans = [[NSAffineTransform alloc] init]; 
    NSGraphicsContext *context = [NSGraphicsContext currentContext]; 
    NSSize sz; 
    NSRect windowFrame = [[self window] frame]; 
    NSRect cv =[[[self window] contentView] frame]; 
    float deltaX, deltaY; 
    NSSize superSize = [[self superview] frame].size; 
    float height, width, sHeight, sWidth; 

    NSRect imageRect; 

    sz = [ image size]; 
    imageRect.size = sz; 
    imageRect.origin = NSZeroPoint; 

    height = sz.height ; 
    width = sz.width ; 

// sHeight and sWidth are the hieght and with of the super-view. ie, 
// the size of the whole window view including the space for the 
// scroll bars, etc, but not including the panel or the borders, 
    sHeight = superSize.height; 
    sWidth = superSize.width; 

    [context saveGraphicsState]; 

    deltaX = 0; 
    deltaY = 0; 

    deltaY += height; // account for flipping 

    [afTrans translateXBy:deltaX yBy:deltaY]; 

    [afTrans scaleXBy:1.0 yBy:-1.0]; 

    [afTrans concat]; 

    NSRect drawingRect = imageRect; 
    NSRect frame = imageRect; 
    [self setFrame:frame]; 

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

    [afTrans release]; 
    [context restoreGraphicsState]; 
} 

ETA는 : 여기에 관련이있을 수있는 몇 가지 더 많은 코드의 여기

코드의 버전을 박탈입니다.

-(void)setImage:(NSImage *)newImage 
{ 
    [newImage retain]; 
    [image release]; 

    rotation = 0; 

    zoom = 1.0; 

    image = newImage; 
    NSSize imageSize = [newImage size]; 
    NSRect tFrame = [self frame]; 
    tFrame = [[self window] frame]; 

    tFrame.size.width = MAX(tFrame.size.width, imageSize.width); 
    tFrame.size.height = MAX(tFrame.size.height, imageSize.height); 
    [self setFrame:tFrame]; 

    [self setNeedsDisplay:YES]; 
} 
+0

[crickets] 나는 이미 tumbleweed 배지를 가지고 있습니다.이 질문이 애매한 경우 B-) –

답변

21

정확히 무엇을 얻으려고하는지 잘 모르겠지만, NSView에 이미지를 그려 왼쪽 상단에 보관하려면 NSView 하위 클래스에서 조금 더 간단하게 할 수 있습니다 :

- (BOOL)isFlipped 
{ 
    return YES; 
} 

- (void)setImage:(NSImage *)newImage 
{ 
    [newImage retain]; 
    [image release]; 
    image = newImage; 

    [image setFlipped:YES]; 
    [self setNeedsDisplay:YES]; 
} 

- (void)drawRect:(NSRect)rect 
{ 
    [image drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1]; 

    // Or stretch image to fill view 
    //[image drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1]; 
} 

가 나는 또한의 drawRect와 setImage 방법에 [self setFrame:...]; 전화 치우는하여 코드와 유사한 결과를 얻을 수 있었다.

+0

그렇지 않으면 스크롤 막대가 올바르지 않았기 때문에 setframes를 추가해야했습니다. 그러나이 단순화는 효과가있었습니다. 감사! –

+0

이미지를 바둑판 식으로 배열하면 어떻게 될까요? – uchuugaka

3

변경 사항이있는 코드를 제거하지 않는다고 가정 할 때 이미지를 다른 곳에서 그리는 경우주의하십시오. 툴바 항목, 표 등은 이미지의 -flipped 속성을 변경하여 잘못 그릴 수 있습니다.

이미지 그리기 라인 주변이 시도 : 그 도움이된다면

BOOL wasFlipped = [image isFlipped]; 
[image setFlipped:[self isFlipped]]; 
[image drawInRect:drawingRect 
      fromRect:imageRect 
      operation:NSCompositeSourceOver 
      fraction:1]; 
[image setFlipped:wasFlipped]; 

을 참조하십시오. 그것은 않는 경우, 이미지의 반전 속성을 변경 뭔가 코드의 다른 곳에서보고 다시 참을 수 없어.

+0

isFlipped 메서드가 변경되지 않고 항상 YES를 반환합니다. 이 코드를 추가하면 (항상 1로 반전되고, 그립니다. 다시 0으로 설정됩니다) UPSIDE DOWN을 시작한 다음 다시 그리면 올바르게 반전됩니다. WTH가 여기에 있습니까? –

+2

다른 일이 일어나야합니다. 위의 코드를 새로운 NSView 클래스에 붙여 넣으면 이미지가 항상 올바르게 그려집니다. 보기의 isFlipped 속성을보기의 isFlipped에서 하드 리턴하는 것 이외의 다른 방법으로 설정하고 있습니까? 그렇다면 isFlipped가 설정되기 전에 초기 그리기가 발생할 수 있습니다. – iKenndac

+0

아니, isFlipped에서 예를 hardsetting ... 그리고 그것은 NSLog 그것을 때 예, 언제나 설정되어있는 것 같습니다 ... 하지만 다른 문제는 그것이 affineTransform, 그 것 같아요 affine 창 크기를 변경하면 변형이 일어나지 않습니다. 적어도, 그것은 아핀 변형을 주석 처리하는 것과 같은 일을합니다. –

관련 문제