NSImageView使用图像大小调整大小(NSImageView resized with image size)

编程入门 行业动态 更新时间:2024-10-28 12:17:17
NSImageView使用图像大小调整大小(NSImageView resized with image size)

我是Obj-C / Cocoa编程的新手,面临着关于NSImageView的问题。 我的目标是创建一个应用程序,允许用户通过拖放操作在其上放置图像。

问题如下:当我们丢弃大于窗口的图像时,它会将窗口大小调整为图像大小, 我不希望这样 ! 我将我的NSImageView放入NSScrollView中,因此只需显示图像的某个部分,用户将移动滑块以查看整个(或简单地调整窗口大小),这就是我想要的...

这是我的XIB层次结构(相关部分):

视图 NSScrollView 视图 NSImageView

在我的NSImageView的代码中,我(关于那个问题):

[self setImageScaling:NSScaleProportionally]; [self setImage:newImage];

注意 :滑块似乎移动视图而不是图像 注意2 :我无法将窗口调整为比图像更小的尺寸

I'm new to Obj-C/Cocoa programming and facing to a problem about NSImageView. My goal is to create an app which allow the user to put an image on it with drag&drop.

The problem is the following : when we're dropping an image larger than the window, it resizes the window to the image size, I don't want that ! I put my NSImageView into a NSScrollView so only a certain part of the image MUST be showed and the user will move sliders to view the entire one (or simply resize the window), that's what I want..

Here is my XIB hierarchy (concerned part) :

View NSScrollView View NSImageView

In the code of my NSImageView, I have (about that problem) :

[self setImageScaling:NSScaleProportionally]; [self setImage:newImage];

NOTE: The sliders seem to move the view and not the image NOTE2: I can't resize the window to a lower size than the image

最满意答案

我对nib文件有一种可怕的厌恶,并且喜欢在可能的情况下以编程方式做事情,所以我对此没什么帮助...但是这里有一个小的NSScrollview测试应用程序列表,我最近编写了类似的东西。 此代码块中提到的唯一非自包含的东西是window名称,它指的是已创建的窗口的NSWindow * 。 而self在这里只是指一个控制器对象。

NSImage * myImage = [NSImage imageNamed:@"myfilename.png"]; NSRect imageRect = NSMakeRect(0.0,0.0,myImage.size.width,myImage.size.height); self.myImageView = [[NSImageView alloc] initWithFrame:imageRect]; self.myImageView.bounds = imageRect; self.myImageView.image = myImage; self.myScrollView = [[NSScrollView alloc] initWithFrame:[window.contentView frame]]; self.myScrollView.hasVerticalScroller = YES; self.myScrollView.hasHorizontalScroller = YES; self.myScrollView.documentView = self.myImageView; self.myScrollView.borderType = NSNoBorder; self.myScrollView.scrollerStyle = NSScrollerStyleOverlay; window.contentView = self.myScrollView;

我知道这可能不是您正在寻找的解决方案,但也许它可以帮助您调试? 特别是,除了给它一个框架,一个边界矩形和一个NSImage *之外,我没有对我的NSImageView做任何有趣的事情,所以我认为你不需要担心缩放参数本身 - 事实上,如果你希望图像是全尺寸的并且可以滚动它,你希望你的NSImageView的框架与图像的尺寸相同,这样它根本不需要扩展。 也许那是你的问题 - 框架太小了? NSImageView帧可以比窗口大 - 这就是让你滚动它的原因。

I have a horrible aversion to nib files and prefer to do things programmatically whenever possible so I'm not much help with that... but here's a little listing from a little NSScrollview test app I recently coded up that does something similar. The only non-self-contained thing mentioned in this code chunk is the window name, which refers to an NSWindow * for a window that was already created. And self here just refers to a controller object.

NSImage * myImage = [NSImage imageNamed:@"myfilename.png"]; NSRect imageRect = NSMakeRect(0.0,0.0,myImage.size.width,myImage.size.height); self.myImageView = [[NSImageView alloc] initWithFrame:imageRect]; self.myImageView.bounds = imageRect; self.myImageView.image = myImage; self.myScrollView = [[NSScrollView alloc] initWithFrame:[window.contentView frame]]; self.myScrollView.hasVerticalScroller = YES; self.myScrollView.hasHorizontalScroller = YES; self.myScrollView.documentView = self.myImageView; self.myScrollView.borderType = NSNoBorder; self.myScrollView.scrollerStyle = NSScrollerStyleOverlay; window.contentView = self.myScrollView;

I know that's perhaps not the solution you were looking for but maybe it helps you debug? In particular, I didn't do anything funny with my NSImageView besides give it a frame, a bounds rectangle, and an NSImage *, so I don't think you need to worry about the scaling parameter per se -- in fact, if you want the image to be full-sized and to scroll around it, you want your NSImageView's frame to be the same size as the image so that it doesn't HAVE to scale at all. Maybe that's your problem -- the frame is too small? It is OK for the NSImageView frame to be bigger than the window -- that's what lets you scroll around it.

更多推荐

本文发布于:2023-04-29 07:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335845.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   图像   NSImageView   size   image

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!