gpt4 book ai didi

cocoa - 如何在imageView的中心绘制一个默认的imageview图像?

转载 作者:行者123 更新时间:2023-12-03 17:33:13 27 4
gpt4 key购买 nike

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

最佳答案

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

// 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];
}
}

关于cocoa - 如何在imageView的中心绘制一个默认的imageview图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1067480/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com