vibetunnel/mac/VibeTunnel/Extensions/NSImage+Resize.swift
Peter Steinberger a9fd66c291 refactor: major project restructuring - move macOS app to mac/ directory
- Move all macOS-specific code from root to mac/ directory
- Move app icons and assets to dedicated assets/ directory
- Update GitHub workflows for new structure
- Consolidate documentation files
- Clean up root directory for better multi-platform organization
2025-06-20 13:20:01 +02:00

37 lines
1.1 KiB
Swift

import AppKit
extension NSImage {
/// Resizes the image to the specified size while maintaining aspect ratio and quality
func resized(to targetSize: NSSize) -> NSImage {
let image = NSImage(size: targetSize)
image.lockFocus()
// Calculate the aspect-fit rectangle
let aspectWidth = targetSize.width / self.size.width
let aspectHeight = targetSize.height / self.size.height
let aspectRatio = min(aspectWidth, aspectHeight)
let scaledWidth = self.size.width * aspectRatio
let scaledHeight = self.size.height * aspectRatio
let drawingRect = NSRect(
x: (targetSize.width - scaledWidth) / 2,
y: (targetSize.height - scaledHeight) / 2,
width: scaledWidth,
height: scaledHeight
)
// Use high-quality interpolation
NSGraphicsContext.current?.imageInterpolation = .high
// Draw the image
self.draw(
in: drawingRect,
from: NSRect(origin: .zero, size: self.size),
operation: .copy,
fraction: 1.0
)
image.unlockFocus()
return image
}
}