screenshots-iphone-island.swift (2254B)
1 #!/usr/bin/env swift 2 3 import CoreGraphics 4 import Foundation 5 import ImageIO 6 7 guard CommandLine.arguments.count >= 2 else { 8 fputs("Usage: add-dynamic-island.swift <image.png> [output.png]\n", stderr) 9 exit(1) 10 } 11 12 let inputPath = CommandLine.arguments[1] 13 let outputPath = CommandLine.arguments.count >= 3 ? CommandLine.arguments[2] : inputPath 14 15 guard let dataProvider = CGDataProvider(filename: inputPath), 16 let image = CGImage( 17 pngDataProviderSource: dataProvider, decode: nil, shouldInterpolate: true, 18 intent: .defaultIntent) 19 else { 20 fputs("Failed to load image: \(inputPath)\n", stderr) 21 exit(1) 22 } 23 24 let width = CGFloat(image.width) 25 let height = CGFloat(image.height) 26 27 // Dynamic Island dimensions as proportions of screen width 28 let pillWidth = width * 0.280 29 let pillHeight = width * 0.062 30 let pillY = width * 0.050 31 let pillX = (width - pillWidth) / 2 32 let cornerRadius = pillHeight / 2 33 34 guard 35 let context = CGContext( 36 data: nil, 37 width: Int(width), 38 height: Int(height), 39 bitsPerComponent: 8, 40 bytesPerRow: 0, 41 space: CGColorSpaceCreateDeviceRGB(), 42 bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue 43 ) 44 else { 45 fputs("Failed to create graphics context\n", stderr) 46 exit(1) 47 } 48 49 // Draw original image 50 context.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height)) 51 52 // Draw Dynamic Island pill (CG origin is bottom-left, so flip Y) 53 let flippedY = height - pillY - pillHeight 54 let pillRect = CGRect(x: pillX, y: flippedY, width: pillWidth, height: pillHeight) 55 let pillPath = CGPath( 56 roundedRect: pillRect, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil) 57 58 context.setFillColor(CGColor(red: 0, green: 0, blue: 0, alpha: 1)) 59 context.addPath(pillPath) 60 context.fillPath() 61 62 guard let resultImage = context.makeImage(), 63 let destination = CGImageDestinationCreateWithURL( 64 URL(fileURLWithPath: outputPath) as CFURL, 65 "public.png" as CFString, 66 1, 67 nil 68 ) 69 else { 70 fputs("Failed to create output\n", stderr) 71 exit(1) 72 } 73 74 CGImageDestinationAddImage(destination, resultImage, nil) 75 guard CGImageDestinationFinalize(destination) else { 76 fputs("Failed to write output\n", stderr) 77 exit(1) 78 }