如何在NSView中实现放大镜
How to implement a magnifier in NSView
我想在 NSView 中实现一个放大镜,它看起来像预览。
我有一些问题
1)是否在NSView中实现?
2)如何实现?
3)如何让放大镜覆盖View?
谢谢你帮助我
我实际上已经这样做了,但我在 objective-C 中做到了。
它实际上需要一些工作才能实现,但简而言之:
- 子类 NSImageView
- 在子类中,您覆盖了 mouseEntered、mouseExited 和 mouseMoved
- 鼠标输入
NSPoint mouseLoc;
BubbleNSImage * magImg;
BubbleNSImage * magGlass;
mouseLoc = [self convertPoint:[event locationInWindow] fromView:nil];
// Setup the magnifying glass image
magGlass = [[NSImage alloc] initWithData:
[[NSImage imageNamed:@"magnifierCursor.png"] TIFFRepresentationUsingCompression:NSTIFFCompressionNone factor:0]];
// Get a 4x zoomed verion of a selection around where the mouse is
// set the image for the image view
if( 0 == _zoomMode ){
magImg = [BubbleNSImage getPixelMultipliedNSImageRegionFromImage:self.image
withMultiplier:4
andSubRegion:NSMakeRect(mouseLoc.x - 17,
self.image.size.height - mouseLoc.y - 17,
34,
34)];
}else{
magImg = [BubbleNSImage getAppleMultipliedNSImageRegionNSImage:self.image
withMultiplier:4
andInterpolationMethod:_zoomMode
andSubRegion:NSMakeRect(mouseLoc.x - 17,
mouseLoc.y - 17,
34,
34)];
}
// Mask the 4x zoomed version with a mask made for the zoomer
[magImg maskWithImage:[NSImage imageNamed:@"magnifierCursorMask.png"]
andMaskOffset:NSMakePoint(0, 0)
andMaskColor:[NSColor colorWithRed:0 green:0 blue:0 alpha:1]];
// Add the 4x zoomed image behind the magnifying glass image
[magGlass addImageBehind:(NSImage *)magImg
withOffset:NSMakePoint(16, 16)
clipToOrigImgSize:NO];
// Preseent the full image as the cursor
[[[NSCursor alloc] initWithImage:(NSImage *)magGlass hotSpot:NSMakePoint(83, 83)] set];
上面代码中的 BubbleNSImage 是 NSImage 的子类,它允许我对标准 NSImage 对象不可用的 NSImages 执行一系列操作。如您所见,它允许我使用 apple 的不同插值器进行缩放,以及使用我个人编写的像素复制添加缩放。它允许我设置缩放倍数,以及我希望缩放的子区域。
稍后它允许我执行遮罩操作(这很有用,因为我也使用了圆形放大镜,并且需要在圆圈外进行剪裁。
然后它允许我执行叠加操作,我将剪裁后的缩放图像放在我的放大镜图像后面。
最后需要用新建的图片初始化一个新的光标对象,然后设置为当前光标。
- 同样的代码需要在重载的 mouseMoved 方法中。
- 您需要在重载的 mouseExited 方法中将光标设置回正常状态
Objective-C代码:
-(void)mouseExited:(NSEvent *)event{
[[NSCursor arrowCursor] set];
}
有相当多的示例代码可用于执行我在上面进行高级算法处理的许多图像处理功能。
我想在 NSView 中实现一个放大镜,它看起来像预览。
我有一些问题
1)是否在NSView中实现?
2)如何实现?
3)如何让放大镜覆盖View?
谢谢你帮助我
我实际上已经这样做了,但我在 objective-C 中做到了。
它实际上需要一些工作才能实现,但简而言之:
- 子类 NSImageView
- 在子类中,您覆盖了 mouseEntered、mouseExited 和 mouseMoved
- 鼠标输入
NSPoint mouseLoc;
BubbleNSImage * magImg;
BubbleNSImage * magGlass;
mouseLoc = [self convertPoint:[event locationInWindow] fromView:nil];
// Setup the magnifying glass image
magGlass = [[NSImage alloc] initWithData:
[[NSImage imageNamed:@"magnifierCursor.png"] TIFFRepresentationUsingCompression:NSTIFFCompressionNone factor:0]];
// Get a 4x zoomed verion of a selection around where the mouse is
// set the image for the image view
if( 0 == _zoomMode ){
magImg = [BubbleNSImage getPixelMultipliedNSImageRegionFromImage:self.image
withMultiplier:4
andSubRegion:NSMakeRect(mouseLoc.x - 17,
self.image.size.height - mouseLoc.y - 17,
34,
34)];
}else{
magImg = [BubbleNSImage getAppleMultipliedNSImageRegionNSImage:self.image
withMultiplier:4
andInterpolationMethod:_zoomMode
andSubRegion:NSMakeRect(mouseLoc.x - 17,
mouseLoc.y - 17,
34,
34)];
}
// Mask the 4x zoomed version with a mask made for the zoomer
[magImg maskWithImage:[NSImage imageNamed:@"magnifierCursorMask.png"]
andMaskOffset:NSMakePoint(0, 0)
andMaskColor:[NSColor colorWithRed:0 green:0 blue:0 alpha:1]];
// Add the 4x zoomed image behind the magnifying glass image
[magGlass addImageBehind:(NSImage *)magImg
withOffset:NSMakePoint(16, 16)
clipToOrigImgSize:NO];
// Preseent the full image as the cursor
[[[NSCursor alloc] initWithImage:(NSImage *)magGlass hotSpot:NSMakePoint(83, 83)] set];
上面代码中的 BubbleNSImage 是 NSImage 的子类,它允许我对标准 NSImage 对象不可用的 NSImages 执行一系列操作。如您所见,它允许我使用 apple 的不同插值器进行缩放,以及使用我个人编写的像素复制添加缩放。它允许我设置缩放倍数,以及我希望缩放的子区域。
稍后它允许我执行遮罩操作(这很有用,因为我也使用了圆形放大镜,并且需要在圆圈外进行剪裁。
然后它允许我执行叠加操作,我将剪裁后的缩放图像放在我的放大镜图像后面。
最后需要用新建的图片初始化一个新的光标对象,然后设置为当前光标。
- 同样的代码需要在重载的 mouseMoved 方法中。
- 您需要在重载的 mouseExited 方法中将光标设置回正常状态
Objective-C代码:
-(void)mouseExited:(NSEvent *)event{
[[NSCursor arrowCursor] set];
}
有相当多的示例代码可用于执行我在上面进行高级算法处理的许多图像处理功能。