容器连接到鼠标位置时图像模糊

问题描述 投票:0回答:1

我正在尝试创建放大视图,但存在一些无法理解的问题。我有包含NSWindowNSImageView。当我将静态原点位置设置为窗口,并且仅移动光标(鼠标)时,所有捕获都很好。但是,当我从鼠标位置将原点设置为窗口时,图像变得非常模糊。

在image.addRepresentation(bitmap)中,我按预期接收到正常图像(已在调试器上检查)。同样在调试模式下,当鼠标移动时,我看到捕获的图像从正常快速缩放到巨大。

有什么建议吗?

NSWindow:

class ColorPickerWindow: NSWindow {

    private let colorPickerMagnifier: ColorPickerMagnifier!

    override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {

        self.colorPickerMagnifier = ColorPickerMagnifier()
        let location = NSEvent.mouseLocation

        super.init(contentRect: NSRect(x: 0, y: 0, width: 120, height: 120),
                   styleMask: [],
                   backing: .buffered,
                   defer: true)

        self.backgroundColor = .clear
        self.level = .floating + 1000
        self.makeKeyAndOrderFront(self)
        self.isMovableByWindowBackground = false
        self.isOpaque = false
        self.hasShadow = false

        NSEvent.addLocalMonitorForEvents(matching: .mouseMoved) { (event) -> NSEvent in

            let location = NSEvent.mouseLocation
            self.updatePickerLocation(location: location)
            print(location)

            return event
        }

        self.colorPickerMagnifier.frame = self.contentView!.bounds
        self.contentView?.addSubview(self.colorPickerMagnifier)        
        self.updatePickerLocation(location: location)
    }

    // MARK: - Private

    private func updatePickerLocation(location: CGPoint) {
        self.updatePickerImage(from: location)
        self.setFrameOrigin(NSPoint(x: location.x - self.contentView!.frame.width / 2, y: location.y - self.contentView!.frame.height / 2))
    }

    private func updatePickerImage(from location: CGPoint) {

        let magnifySize = CGFloat(8)
        let x = floor(location.x) - floor(magnifySize / 2)
        let y = (NSScreen.main?.frame)!.size.height - floor(location.y) - floor(magnifySize / 2)
        let captureRect = CGRect(x: x, y: y, width: magnifySize, height: magnifySize)

        guard let cgImageFromScreen = CGWindowListCreateImage(captureRect, .optionOnScreenAboveWindow, .zero, .bestResolution) else {
            return
        }

        let bitmap = NSBitmapImageRep(cgImage: cgImageFromScreen)
        let image = NSImage()
        image.addRepresentation(bitmap)
        self.colorPickerMagnifier.image = image
    }
}

NSImageView:

class ColorPickerMagnifier: ImageViewDeInterpolated {

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)

        self.imageScaling = .scaleProportionallyUpOrDown
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
    }
}

仅移动鼠标时的内容Content when moving only mouse

容器连接到鼠标位置时的内容Content when container attached to mouse position

swift nswindow nsimageview swift5.2
1个回答
0
投票

https://github.com/dagronf/DSFColorSampler上找到了源和解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.