我有一个简单的OBJ C程序,此刻,让你加载图像,绘制起来,理论上应该让你缩放和旋转。我使用NSAffineTranslations。

我想要的图像被锁定到左上(相对于的左下方的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];
}

埃塔:这里的一些更多的代码,可能是相关的

-(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];
}
有帮助吗?

解决方案

我不知道你想实现什么,但如果你想只绘制图像中的一个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:...];电话让你的代码相似的结果。

其他提示

假设你没有剥离出来的代码改变的东西,注意如果你在其他地方绘制图像。工具栏项,表格等可能会改变的图像的属性-flipped,导致它错误地绘制。

试试这个图像周围绘制行:

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

请参阅是否有帮助。如果它的的,其他地方寻找在你的代码的东西,改变图像的翻转属性,而不是把它回来了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top