Cocoa:在 Swift 中保存时捕获屏幕和缩放图像

Cocoa: Capture Screen and scale image on saving in Swift

下面是我用来在 macOS 应用程序中捕获屏幕的代码,

let img = CGDisplayCreateImage(CGMainDisplayID())

guard let destination = FileManager.default.urls(for: .downloadsDirectory,
    in: .userDomainMask).first?.appendingPathComponent("shot.jpg", isDirectory: false)
else {
    print("Unable to save captured image!")
    return
}
            
let properties: CFDictionary = [
    kCGImagePropertyPixelWidth: "900",
    kCGImagePropertyPixelHeight: "380"
] as CFDictionary
            
if let dest = CGImageDestinationCreateWithURL(destination as CFURL, kUTTypeJPEG, 1, properties) {
    CGImageDestinationAddImage(dest, img!, properties)
    CGImageDestinationFinalize(dest)
}
else {
    print("Unable to create captured image to the destination!")
}

我必须在保存时将图像缩放到特定尺寸。所以,我使用 CFDictionary 和图像的 width, heigh 属性。但似乎我做错了。请帮我找出正确的解决方案。谢谢!

首先,您无法使用 CGImageDestinationCreateWithURLCGImageDestinationAddImage 调整大小。如果您查看文档 here and here,您会注意到 kCGImagePropertyPixelWidthkCGImagePropertyPixelHeight 均不受支持。

您需要手动调整大小。如果您觉得有帮助,可以使用或修改此工具。它支持填充(拉伸)和适合(在保持原始纵横比的同时缩放)内容模式。如果您指定 .fit,它将使绘图在生成的图像中居中。如果你指定 .fill 它将填充整个 space 拉伸它需要的任何维度。

enum ImageResizer {

    enum ContentMode {
        case fill
        case fit
    }

    enum Error: Swift.Error {
        case badOriginal
        case resizeFailed
    }

    static func resize(_ source: CGImage, to targetSize: CGSize, mode: ContentMode) throws -> CGImage {

        let context = CGContext(
            data: nil,
            width: Int(targetSize.width),
            height: Int(targetSize.height),
            bitsPerComponent: source.bitsPerComponent,
            bytesPerRow: 0,
            space: source.colorSpace ?? CGColorSpace(name: CGColorSpace.sRGB)!,
            bitmapInfo: source.bitmapInfo.rawValue
        )

        guard let context = context else {
            throw Error.badOriginal
        }

        let drawingSize: CGSize
        switch mode {
        case .fill:
            drawingSize = targetSize
        case .fit:
            drawingSize = CGSize(width: source.width, height: source.height)
                .scaledToFit(target: targetSize)
        }

        let drawRect = CGRect(origin: .zero, size: targetSize)
            .makeCenteredRect(withSize: drawingSize)

        context.interpolationQuality = .high
        context.draw(source, in: drawRect)

        guard let result = context.makeImage() else {
            throw Error.resizeFailed
        }

        return result
    }
}

ImageResizer 取决于这些 CG 扩展来缩放源图像和居中缩放图像:

extension CGSize {

    var maxDimension: CGFloat {
        Swift.max(width, height)
    }

    var minDimension: CGFloat {
        Swift.min(width, height)
    }

    func scaled(by scalar: CGFloat) -> CGSize {
        CGSize(width: width * scalar, height: height * scalar)
    }

    func scaleFactors(to target: CGSize) -> CGSize {
        CGSize(
            width: target.width / width,
            height: target.height / height
        )
    }

    func scaledToFit(target: CGSize) -> CGSize {
        return scaled(by: scaleFactors(to: target).minDimension)
    }
}

extension CGRect {
    func makeCenteredRect(withSize size: CGSize) -> CGRect {
        let origin = CGPoint(
            x: midX - size.width / 2.0,
            y: midY - size.height / 2.0
        )
        return CGRect(origin: origin, size: size)
    }
}

此外,如果要保存到 .downloadsDirectory,请确保设置