0

如何在 imageView 中心的 imageview 中绘制默认图像?使用 - (void)drawRect:(NSRect)rect 重写的 NSImageView 方法

4

2 回答 2

3

是的。那是一种方式。我使用了以下代码。

// Drawing 

   - (void)drawRect:(NSRect)rect
{
    if([self image])
    {       
        [[NSColor grayColor] set];
        NSRectFill(rect);

        //ImageView Bounds and Size
        NSRect vBounds = [self bounds];
        NSSize vSize = vBounds.size;

        //Get the size and origin of default image set to imageView
        NSRect imageRect;
        imageRect.size = [[self image] size];
        imageRect.origin = NSZeroPoint;

        //Create a preview image
        NSSize previewSize = NSMakeSize( [self image].width / 4.0, [self image].height / 4.0 );
        NSImage *previewImage = [[NSImage alloc] initWithSize:previewSize];


        //Get the point where the preview image needs to be draw
        NSRect newRect;
        newRect.origin.x = vSize.width/2-previewSize.width/2;
        newRect.origin.y = vSize.height/2-previewSize.height/2;
        newRect.size = [previewImage size];

        //Draw preview image in imageView
        [[self image] drawInRect:newRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1.0];

        [previewImage release]; 
    }
}
于 2009-07-01T07:38:51.080 回答
1

为什么不先将图像视图的图像设置为默认图像,然后再更改呢?

于 2009-07-01T07:16:32.560 回答