Scripts/screenshots-iphone-island.swift (2254B)
1
2
3import CoreGraphics
4import Foundation
5import ImageIO
6
7guard CommandLine.arguments.count >= 2 else {
8 fputs("Usage: add-dynamic-island.swift <image.png> [output.png]\n", stderr)
9 exit(1)
10}
11
12let inputPath = CommandLine.arguments[1]
13let outputPath = CommandLine.arguments.count >= 3 ? CommandLine.arguments[2] : inputPath
14
15guard let dataProvider = CGDataProvider(filename: inputPath),
16 let image = CGImage(
17 pngDataProviderSource: dataProvider, decode: nil, shouldInterpolate: true,
18 intent: .defaultIntent)
19else {
20 fputs("Failed to load image: \(inputPath)\n", stderr)
21 exit(1)
22}
23
24let width = CGFloat(image.width)
25let height = CGFloat(image.height)
26
27
28let pillWidth = width * 0.280
29let pillHeight = width * 0.062
30let pillY = width * 0.050
31let pillX = (width - pillWidth) / 2
32let cornerRadius = pillHeight / 2
33
34guard
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 )
44else {
45 fputs("Failed to create graphics context\n", stderr)
46 exit(1)
47}
48
49
50context.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height))
51
52
53let flippedY = height - pillY - pillHeight
54let pillRect = CGRect(x: pillX, y: flippedY, width: pillWidth, height: pillHeight)
55let pillPath = CGPath(
56 roundedRect: pillRect, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil)
57
58context.setFillColor(CGColor(red: 0, green: 0, blue: 0, alpha: 1))
59context.addPath(pillPath)
60context.fillPath()
61
62guard let resultImage = context.makeImage(),
63 let destination = CGImageDestinationCreateWithURL(
64 URL(fileURLWithPath: outputPath) as CFURL,
65 "public.png" as CFString,
66 1,
67 nil
68 )
69else {
70 fputs("Failed to create output\n", stderr)
71 exit(1)
72}
73
74CGImageDestinationAddImage(destination, resultImage, nil)
75guard CGImageDestinationFinalize(destination) else {
76 fputs("Failed to write output\n", stderr)
77 exit(1)
78}