Swift 4中如何消除自定义相机启动前的白屏问题
Hey there! I’ve dealt with this exact white screen glitch when building custom camera features for iOS apps, so let me share the practical fixes that resolved it for me:
Pre-initialize your AVCaptureSession early
Don’t wait untilviewDidLoadorviewWillAppearto set up your capture session. Instead, kick off session initialization in a background thread before the user navigates to the camera screen. For example, you could start setup in the previous view controller’sviewDidAppear, or use a singleton camera manager that preloads the session when the app launches. This way, by the time the user opens the camera, the session is already warmed up and ready to go.Replace the white screen with a camera-aligned placeholder
If you can’t eliminate the delay entirely, mask it by setting your camera view controller’s initial background to match the camera’s default state. Addself.view.backgroundColor = .blackinviewDidLoad, or use a static placeholder image that mimics a camera preview (like a dark frame with a shutter button overlay). This makes the transition feel seamless instead of showing a jarring white flash.Offload session setup to a background thread
AVCaptureSession configuration can block the main thread if done synchronously, causing the white screen. Move all heavy setup work to a background queue, then update the UI on the main thread once the session is ready:DispatchQueue.global(qos: .userInitiated).async { self.captureSession = AVCaptureSession() self.captureSession.sessionPreset = .photo // Add your camera input, photo output, etc. here self.captureSession.startRunning() DispatchQueue.main.async { self.previewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession) self.previewLayer.frame = self.view.bounds self.view.layer.addSublayer(self.previewLayer) // Show your camera controls here } }Delay non-critical UI initialization
If your camera screen has extra UI elements (filters, settings panels, etc.), don’t load them all at launch. Wait until the capture session is running (listen for the.AVCaptureSessionDidStartRunningnotification) before adding these elements. Prioritize getting the preview layer on screen first.Use KVO to monitor session state
Add a KVO observer to track when the capture session transitions to the running state, then trigger UI updates only after that happens. This ensures you don’t show empty UI before the camera is ready:override func viewDidLoad() { super.viewDidLoad() captureSession.addObserver(self, forKeyPath: "running", options: .new, context: nil) } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if keyPath == "running" { let isRunning = change?[.newKey] as? Bool ?? false if isRunning { // Hide placeholders, show camera controls } } }
Combining these steps should eliminate or mask that annoying one-second white screen, giving users the impression that the camera opens instantly.
内容的提问来源于stack exchange,提问作者Pratz_iOS

