我有一個簡單的Obj C程序,目前它允許您加載圖像,繪製圖像,理論上可以讓您縮放和旋轉。我正在使用NSAffineTranslations。我想要圖像被鎖定在左上角(與左下角的PS/PDF標準相反),所以我使用isFlipped,並調用[afTrans scaleXBy:1.0 yBy:-1.0];在NSView中繪製圖像
問題是,由於某種原因,第一次調用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];
}
埃塔:這裏的一些代碼,可能是相關的。
-(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];
}
[蟋蟀]我已經有風滾草徽章......如果這是一個晦澀難懂的問題,我覺得自己沒有想出來更好 - ) – 2009-08-12 17:32:44