围绕某些 NSRect 剪切 NSImage

发布于 2024-09-11 08:52:08 字数 86 浏览 13 评论 0原文

我有一个 NSImage,里面有不同的图像。它内部图像的位置将始终保持不变,因此我需要指定一个矩形以在该矩形位于图像内部的位置处获取子图像。这怎么可能做到呢?

I have an NSImage with different images inside it. The positions of the images inside of it will stay the same all the time, so I need to specify a rectangle to get a subimage at the position of the rectangle is inside of the image. How could this be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2024-09-18 08:52:08

为什么一张图像里面有多个图像?过早优化?

无论如何,当您 绘制图像,你传递的参数之一就是你想要的图像的部分。通常,您可以传递图像的边界或零矩形(两者含义相同),但如果需要,您可以传递子矩形。

如果目标矩形比源矩形大,它将按比例放大(不按比例);如果较小,则会按比例缩小(不按比例)。如果您不想进行任何缩放,请调整目标矩形的大小以匹配源矩形的大小。

如果要将绘图留给 NSImageView,创建所需大小的空 NSImage,然后绘制到其中,然后将该图像传递到图像视图。这可能会破坏您希望通过将这些全部混合到一个图像中而获得的任何性能优势。

您还可以创建 NSView、NSImageView 或 CALayer 的自定义子类,它具有 NSImage 属性和源矩形属性,并且仅绘制该部分。

Why are there multiple images inside of one image? Premature optimization?

In any case, when you draw the image, one of the parameters you pass is the section of the image you want. Normally, you pass either the bounds of the image or the zero rect (which both mean the same thing), but you can pass a subrectangle if you want.

If the destination rectangle is larger than the source rectangle, it will scale up (non-proportionally); if it's smaller, it will scale down (non-proportionally). Adjust the size of the destination rectangle to match that of the source rectangle if you don't want any scaling.

If you want to leave the drawing to an NSImageView, create an empty NSImage of the desired size, then draw into it, then pass that image to the image view. This probably destroys whatever performance advantage you'd hoped to gain by mashing these all together into one image.

You could also create a custom subclass of NSView, NSImageView, or CALayer that has a property for the NSImage and a property for the source rectangle, and draws only that section.

尐籹人 2024-09-18 08:52:08

从 macOS 10.6 开始,这很容易。
此代码采用包含多个剪辑的主图像
并提取该母版的一部分以生成剪切图像。

// masterImage is the source image that contains multipe clips
NSImage *masterImage = [NSImage imageNamed:@"multiple_images.jpg"];
NSRect mFrame = NSMakeRect(0, 0, masterImage.size.width, masterImage.size.height);
NSImageRep *representation = [masterImage bestRepresentationForRect:mFrame
                                                            context:nil
                                                              hints:nil];

#define TARGET_IMAGE_SIZE    40 // NSImageView size (where the clip will be used)
NSSize targetSize = NSMakeSize(TARGET_IMAGE_SIZE, TARGET_IMAGE_SIZE);
NSImage *clippedImage = [NSImage imageWithSize:targetSize
                                       flipped:NO
                                drawingHandler:^BOOL(NSRect dstRect) {

    // set clipRect according to which clip you want:
    float xPos = 0; // compute these ...
    float yPos = 0; // ... positions of the clip within masterImage
    float clipSize = 50; // assumes each clip is square
    NSRect clipRect = NSMakeRect(xPos, yPos, clipSize, clipSize);

    BOOL ret = [representation drawInRect:dstRect
                                 fromRect:clipRect
                                operation:NSCompositingOperationCopy
                                 fraction:1.0 // alpha
                           respectFlipped:NO
                                    hints:nil];
    return ret; // ret from drawingHandler
}];

NSRect ivFrame = NSMakeRect( 0, 0, TARGET_IMAGE_SIZE, TARGET_IMAGE_SIZE);
NSImageView *clipImageView = [[NSImageView alloc] initWithFrame:ivFrame];
clipImageView.image = clippedImage;

提示:使用循环,布局 'N' NSImageViews
(其中“N”是剪辑数量),
将每个剪辑提取到其中一个 imageView 中,
并弄清楚如何计算每个剪辑的 xPos、yPos 和剪辑大小 (clipRect)。
当您可以一次查看所有剪辑时会更容易。

This is easy as of macOS 10.6.
This code takes a master image containing multiple clips
and extracts part of that master to produce a clipped image.

// masterImage is the source image that contains multipe clips
NSImage *masterImage = [NSImage imageNamed:@"multiple_images.jpg"];
NSRect mFrame = NSMakeRect(0, 0, masterImage.size.width, masterImage.size.height);
NSImageRep *representation = [masterImage bestRepresentationForRect:mFrame
                                                            context:nil
                                                              hints:nil];

#define TARGET_IMAGE_SIZE    40 // NSImageView size (where the clip will be used)
NSSize targetSize = NSMakeSize(TARGET_IMAGE_SIZE, TARGET_IMAGE_SIZE);
NSImage *clippedImage = [NSImage imageWithSize:targetSize
                                       flipped:NO
                                drawingHandler:^BOOL(NSRect dstRect) {

    // set clipRect according to which clip you want:
    float xPos = 0; // compute these ...
    float yPos = 0; // ... positions of the clip within masterImage
    float clipSize = 50; // assumes each clip is square
    NSRect clipRect = NSMakeRect(xPos, yPos, clipSize, clipSize);

    BOOL ret = [representation drawInRect:dstRect
                                 fromRect:clipRect
                                operation:NSCompositingOperationCopy
                                 fraction:1.0 // alpha
                           respectFlipped:NO
                                    hints:nil];
    return ret; // ret from drawingHandler
}];

NSRect ivFrame = NSMakeRect( 0, 0, TARGET_IMAGE_SIZE, TARGET_IMAGE_SIZE);
NSImageView *clipImageView = [[NSImageView alloc] initWithFrame:ivFrame];
clipImageView.image = clippedImage;

Tip: using a loop, layout 'N' NSImageViews
(where 'N' is the number of clips),
extract each clip into one of those imageViews,
and figure out how to compute the xPos, yPos, and clip size (clipRect) for each clip.
It's easier when you can see all of the clips at once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文