样本NSImage并保持质量(快速3)

时间:2016-12-21 17:33:31

标签: swift macos appkit

我写了一个NSImage扩展,允许我随机抽取图像。我希望这些样本保持与原始图像相同的质量。但是,它们似乎有别名或略显模糊。这是一个例子 - 右侧绘制的原始图像和左侧的随机样本:

sample

我现在在SpriteKit中玩这个。以下是我创建原始图片的方法:

    let bg = NSImage(imageLiteralResourceName: "ref")
    let tex = SKTexture(image: bg)
    let sprite = SKSpriteNode(texture: tex)
    sprite.position = CGPoint(x: size.width/2, y:size.height/2)
    addChild(sprite)

以下是我创建示例的方法:

    let sample = bg.sample(size: NSSize(width: 100, height: 100))
    let sampletex = SKTexture(image:sample!)
    let samplesprite = SKSpriteNode(texture:sampletex)
    samplesprite.position = CGPoint(x: 60, y:size.height/2)
    addChild(samplesprite)

这里是创建样本的NSImage扩展(和randomNumber func):

extension NSImage {

    /// Returns the height of the current image.
    var height: CGFloat {
        return self.size.height
    }

    /// Returns the width of the current image.
    var width: CGFloat {
        return self.size.width
    }

    func sample(size: NSSize) -> NSImage? {
        // Resize the current image, while preserving the aspect ratio.
        let source = self

        // Make sure that we are within a suitable range
        var checkedSize    = size
        checkedSize.width  = floor(min(checkedSize.width,source.size.width * 0.9))
        checkedSize.height = floor(min(checkedSize.height, source.size.height * 0.9))

        // Get random points for the crop.
        let x = randomNumber(range: 0...(Int(source.width) - Int(checkedSize.width)))
        let y = randomNumber(range: 0...(Int(source.height) - Int(checkedSize.height)))

        // Create the cropping frame.
        var frame = NSRect(x: x, y: y, width: Int(checkedSize.width), height: Int(checkedSize.height))

        // let ref = source.cgImage.cropping(to:frame)
        let ref = source.cgImage(forProposedRect: &frame, context: nil, hints: nil)
        let rep = NSBitmapImageRep(cgImage: ref!)

        // Create a new image with the new size
        let img = NSImage(size: checkedSize)

        // Set a graphics context
        img.lockFocus()
        defer { img.unlockFocus() }

        // Fill in the sample image
        if rep.draw(in: NSMakeRect(0, 0, checkedSize.width, checkedSize.height),
                    from: frame,
                    operation: NSCompositingOperation.copy,
                    fraction: 1.0,
                    respectFlipped: false,
                    hints: [NSImageHintInterpolation:NSImageInterpolation.high.rawValue]) {
            // Return the cropped image.
            return img
        }

        // Return nil in case anything fails.
        return nil
    }

}

func randomNumber(range: ClosedRange<Int> = 0...100) -> Int {
    let min = range.lowerBound
    let max = range.upperBound
    return Int(arc4random_uniform(UInt32(1 + max - min))) + min
}

我已经尝试了大约10种不同的方式,结果似乎总是有点模糊的样本。我甚至检查了屏幕上的污迹。 :)

如何创建保留原始源图像部分精确质量的NSImage样本?

1 个答案:

答案 0 :(得分:1)

在这种情况下,将插值模式切换为NSImageInterpolation.none显然已足够。

正确处理绘图目标rect也很重要。由于cgImage(forProposedRect:...)可能会更改建议的rect,因此您应该使用基于它的目标rect。基本上你应该使用{(1}}的副本,它的偏移量为(-x,-y),因此它相对于(0,0)而不是(x,y)。