import UIKit @main final class AppDelegate:UIResponder,UIApplicationDelegate { func application(_ app:UIApplication,configurationForConnecting session:UISceneSession,options:UIScene.ConnectionOptions)->UISceneConfiguration { let c=UISceneConfiguration(name:nil,sessionRole:session.role);c.delegateClass=SceneDelegate.self;return c } } final class SceneDelegate:UIResponder,UIWindowSceneDelegate { var window:UIWindow? func scene(_ scene:UIScene,willConnectTo session:UISceneSession,options:UIScene.ConnectionOptions) { guard let scene=scene as? UIWindowScene else{return} let w=UIWindow(windowScene:scene);w.rootViewController=ProbeController();w.makeKeyAndVisible();window=w } } final class ProbeController:UIViewController { let status=UILabel();let source=UIView(frame:CGRect(x:40,y:200,width:240,height:160)) override func viewDidLoad() { super.viewDidLoad();view.backgroundColor = .white status.frame=CGRect(x:25,y:90,width:350,height:90);status.numberOfLines=0;status.text="Replica readback: preparing";view.addSubview(status) source.backgroundColor = .red let label=UILabel(frame:CGRect(x:15,y:50,width:210,height:45));label.text="Ordinary UIKit";label.textColor = .white;source.addSubview(label);view.addSubview(source) Task {@MainActor in try? await Task.sleep(nanoseconds:2_000_000_000);await run()} } @MainActor func run() async { guard let window=view.window else{return} let directory=FileManager.default.urls(for:.documentDirectory,in:.userDomainMask)[0] var results=[[String:Any]]() let format=UIGraphicsImageRendererFormat();format.scale=1;format.opaque=false func record(_ target:UIView,_ name:String,_ sentinel:Bool) { var success=false let image=UIGraphicsImageRenderer(size:target.bounds.size,format:format).image {ctx in if sentinel {UIColor.cyan.setFill();ctx.fill(target.bounds)} success=target.drawHierarchy(in:target.bounds,afterScreenUpdates:true) } guard let cg=image.cgImage else{return} let width=cg.width,height=cg.height var bytes=[UInt8](repeating:0,count:width*height*4) bytes.withUnsafeMutableBytes {ptr in let c=CGContext(data:ptr.baseAddress,width:width,height:height,bitsPerComponent:8,bytesPerRow:width*4,space:CGColorSpace(name:CGColorSpace.sRGB)!,bitmapInfo:CGImageAlphaInfo.premultipliedLast.rawValue)! c.draw(cg,in:CGRect(x:0,y:0,width:width,height:height)) } var alpha=0,red=0,cyan=0 for i in stride(from:0,to:bytes.count,by:4) { if bytes[i+3]>0 {alpha+=1} if bytes[i]>150 && bytes[i+1]<100 && bytes[i+3]>0 {red+=1} if bytes[i]<10 && bytes[i+1]>245 && bytes[i+2]>245 && bytes[i+3]>0 {cyan+=1} } results.append(["name":name,"success":success,"pixels":width*height,"nonzeroAlpha":alpha,"redPixels":red,"cyanPixels":cyan,"layer":String(describing:type(of:target.layer)),"contentsType":String(describing:target.layer.contents.map{type(of:$0)})]) try? image.pngData()?.write(to:directory.appendingPathComponent(name+".png")) } record(source,"original-clear",false);record(source,"original-cyan",true) for kind in ["view","window","screen"] { source.isHidden=false try? await Task.sleep(nanoseconds:500_000_000) let replica:UIView switch kind { case "view":guard let s=source.snapshotView(afterScreenUpdates:false) else{continue};replica=s;replica.frame=source.frame case "window":guard let s=window.snapshotView(afterScreenUpdates:false) else{continue};replica=s;replica.frame=window.bounds default:replica=window.screen.snapshotView(afterScreenUpdates:false);replica.frame=window.bounds } window.addSubview(replica);try? await Task.sleep(nanoseconds:500_000_000) source.isHidden=true;try? await Task.sleep(nanoseconds:500_000_000) record(replica,kind+"-clear",false);record(replica,kind+"-cyan",true) let marker=UIView(frame:CGRect(x:10,y:10,width:30,height:30));marker.backgroundColor = .red;replica.addSubview(marker) try? await Task.sleep(nanoseconds:500_000_000) record(replica,kind+"-child-marker-cyan",true) replica.removeFromSuperview() } source.isHidden=false let output:[String:Any]=["os":UIDevice.current.systemVersion,"model":UIDevice.current.model,"screenScale":window.screen.scale,"compiledSimulator":isSimulator,"configuration":"Release without XCTest or debugger","measurementsBeforePNGEncoding":results] if let data=try? JSONSerialization.data(withJSONObject:output,options:[.prettyPrinted,.sortedKeys]) {try? data.write(to:directory.appendingPathComponent("results.json"))} status.text="Finished. Results saved.\nOriginal + view/window/screen replicas." } var isSimulator:Bool { #if targetEnvironment(simulator) return true #else return false #endif } }