


















@@ -284,6 +284,7 @@ def normalize_watch_screenshot_status_bar(path)
284284script = <<~SWIFT
285285 import AppKit
286286 import Foundation
287+ import ImageIO
287288288289 let path = CommandLine.arguments[1]
289290 let timeText = CommandLine.arguments[2]
@@ -295,36 +296,37 @@ def normalize_watch_screenshot_status_bar(path)
295296 exit(2)
296297 }
297298298- let width = CGFloat(cgImage.width)
299- let height = CGFloat(cgImage.height)
300- guard let bitmap = NSBitmapImageRep(
301- bitmapDataPlanes: nil,
302- pixelsWide: Int(width),
303- pixelsHigh: Int(height),
304- bitsPerSample: 8,
305- samplesPerPixel: 4,
306- hasAlpha: true,
307- isPlanar: false,
308- colorSpaceName: .deviceRGB,
309- bytesPerRow: 0,
310- bitsPerPixel: 0),
311- let graphicsContext = NSGraphicsContext(bitmapImageRep: bitmap)
299+ let width = cgImage.width
300+ let height = cgImage.height
301+ let drawWidth = CGFloat(width)
302+ let drawHeight = CGFloat(height)
303+ let colorSpace = CGColorSpace(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB()
304+ guard let bitmapContext = CGContext(
305+ data: nil,
306+ width: width,
307+ height: height,
308+ bitsPerComponent: 8,
309+ bytesPerRow: width * 4,
310+ space: colorSpace,
311+ bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue)
312312 else {
313313 fputs("Failed to create normalized screenshot bitmap at \\(path)\\n", stderr)
314314 exit(3)
315315 }
316316317- bitmap.size = NSSize(width: width, height: height)
317+ let graphicsContext = NSGraphicsContext(cgContext: bitmapContext, flipped: false)
318318 NSGraphicsContext.saveGraphicsState()
319319 NSGraphicsContext.current = graphicsContext
320+ NSColor.black.setFill()
321+ NSBezierPath(rect: NSRect(x: 0, y: 0, width: drawWidth, height: drawHeight)).fill()
320322 source.draw(
321- in: NSRect(x: 0, y: 0, width: width, height: height),
322- from: NSRect(x: 0, y: 0, width: width, height: height),
323- operation: .copy,
323+ in: NSRect(x: 0, y: 0, width: drawWidth, height: drawHeight),
324+ from: NSRect(x: 0, y: 0, width: drawWidth, height: drawHeight),
325+ operation: .sourceOver,
324326 fraction: 1.0)
325327326328 NSColor.black.setFill()
327- NSBezierPath(rect: NSRect(x: width - 146, y: height - 92, width: 124, height: 70)).fill()
329+ NSBezierPath(rect: NSRect(x: drawWidth - 146, y: drawHeight - 92, width: 124, height: 70)).fill()
328330329331 let paragraphStyle = NSMutableParagraphStyle()
330332 paragraphStyle.alignment = .right
@@ -334,17 +336,26 @@ def normalize_watch_screenshot_status_bar(path)
334336 .paragraphStyle: paragraphStyle,
335337 ]
336338 timeText.draw(
337- in: NSRect(x: width - 134, y: height - 82, width: 102, height: 44),
339+ in: NSRect(x: drawWidth - 134, y: drawHeight - 82, width: 102, height: 44),
338340 withAttributes: attributes)
339341 NSGraphicsContext.restoreGraphicsState()
340342341- guard let png = bitmap.representation(using: .png, properties: [:])
343+ guard let output = bitmapContext.makeImage(),
344+ let destination = CGImageDestinationCreateWithURL(
345+ URL(fileURLWithPath: path) as CFURL,
346+ "public.png" as CFString,
347+ 1,
348+ nil)
342349 else {
343350 fputs("Failed to encode normalized screenshot at \\(path)\\n", stderr)
344351 exit(4)
345352 }
346353347- try png.write(to: URL(fileURLWithPath: path))
354+ CGImageDestinationAddImage(destination, output, nil)
355+ guard CGImageDestinationFinalize(destination) else {
356+ fputs("Failed to write normalized screenshot at \\(path)\\n", stderr)
357+ exit(5)
358+ }
348359 SWIFT
349360350361Tempfile.create(["openclaw-watch-status-bar", ".swift"]) do |file|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。