From c63d14d99a259129217202f9a506638fc00b9aa9 Mon Sep 17 00:00:00 2001 From: Leo Mehlig Date: Wed, 27 Jan 2016 16:16:37 +0100 Subject: [PATCH] Fixed wrong image size on retina with external monitor MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was a bug were the icon were the wrong size, because it used the build-in monitor’s scaleFactor to draw but the mainScreen was the external monitor, resulting in doubled sized icons --- Scripts/icon_processor.swift | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Scripts/icon_processor.swift b/Scripts/icon_processor.swift index f3a09a7..2a86216 100755 --- a/Scripts/icon_processor.swift +++ b/Scripts/icon_processor.swift @@ -3,10 +3,8 @@ import Accelerate import CoreGraphics func image(withImage image:NSImage, buildVersion:String, buildNumber:String, buildType:String) -> NSImage { - //Generate blured image - let imageScale = image.recommendedLayerContentsScale(1) - let screenScale = NSScreen.mainScreen()!.backingScaleFactor - let size = CGSize(width: image.size.width / screenScale * imageScale, height: image.size.height / screenScale * imageScale) + let imageScale: CGFloat = image.recommendedLayerContentsScale(1) + let size = CGSize(width: image.size.width * imageScale, height: image.size.height * imageScale) let bluredImage = image.applyBlurWithRadius(size.width * 0.1, tintColor: NSColor(white: 0.11, alpha: 0.3), saturationDeltaFactor: 1.8) let bounds = CGRect(origin: CGPointZero, size: size) @@ -66,8 +64,23 @@ public extension String { public extension NSImage { public func saveAsPNGatPath(path:String, atomically: Bool = true) -> Bool { - let data = self.TIFFRepresentationUsingCompression(NSTIFFCompression.None, factor: 1.0)! - let bitmap = NSBitmapImageRep(data: data)! + let bitmap = NSBitmapImageRep( + bitmapDataPlanes: nil, + pixelsWide: Int(self.size.width), + pixelsHigh: Int(self.size.height), + bitsPerSample: 8, + samplesPerPixel: 4, + hasAlpha: true, + isPlanar: false, + colorSpaceName: NSCalibratedRGBColorSpace, + bytesPerRow: 0, + bitsPerPixel: 0)! + bitmap.size = self.size + + NSGraphicsContext.saveGraphicsState() + NSGraphicsContext.setCurrentContext(NSGraphicsContext(bitmapImageRep: bitmap)) + self.drawInRect(CGRect(origin: CGPoint(), size: size), fromRect: CGRect(), operation: .CompositeCopy, fraction: 1.0) + NSGraphicsContext.restoreGraphicsState() if let imagePGNData: NSData = bitmap.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: [NSImageCompressionFactor: 1.0]) { return imagePGNData.writeToFile(NSString(string: path).stringByStandardizingPath, atomically: atomically) } else {