杨锴
2025-03-11 90dc3329d1973fda691e357cf4523d5c7c67fa1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// NSImageExtensions.swift - Copyright 2024 SwifterSwift
 
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import AppKit
 
// MARK: - Methods
 
public extension NSImage {
    /// SwifterSwift: NSImage scaled to maximum size with respect to aspect ratio.
    ///
    /// - Parameter maxSize: maximum size
    /// - Returns: scaled NSImage
    func scaled(toMaxSize maxSize: NSSize) -> NSImage {
        let imageWidth = size.width
        let imageHeight = size.height
 
        guard imageHeight > 0 else { return self }
 
        // Get ratio (landscape or portrait)
        let ratio: CGFloat = if imageWidth > imageHeight {
            // Landscape
            maxSize.width / imageWidth
        } else {
            // Portrait
            maxSize.height / imageHeight
        }
 
        // Calculate new size based on the ratio
        let newWidth = imageWidth * ratio
        let newHeight = imageHeight * ratio
 
        // Create a new NSSize object with the newly calculated size
        let newSize = NSSize(width: newWidth.rounded(.down), height: newHeight.rounded(.down))
 
        // Cast the NSImage to a CGImage
        var imageRect = CGRect(origin: .zero, size: size)
        guard let imageRef = cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else { return self }
 
        return NSImage(cgImage: imageRef, size: newSize)
    }
 
    /// SwifterSwift: Write NSImage to url.
    ///
    /// - Parameters:
    ///   - url: Desired file URL.
    ///   - type: Type of image (default is .jpeg).
    ///   - compressionFactor: used only for JPEG files. The value is a float between 0.0 and 1.0, with 1.0 resulting in
    /// no compression and 0.0 resulting in the maximum compression possible.
    func write(to url: URL, fileType type: NSBitmapImageRep.FileType = .jpeg, compressionFactor: NSNumber = 1.0) {
        // https://stackoverflow.com/a/45042611/3882644
 
        guard let data = tiffRepresentation else { return }
        guard let imageRep = NSBitmapImageRep(data: data) else { return }
 
        guard let imageData = imageRep.representation(using: type, properties: [.compressionFactor: compressionFactor]) else { return }
        try? imageData.write(to: url)
    }
}
 
#endif