简体   繁体   中英

How can I update the contents of a CALayer while the main thread is blocked?

An instance of AVCaptureVideoPreviewLayer continues to update its contents from the video capture stream even while the main thread is blocked. Is it possible to generally replicate this behaviour with a custom subclass of CALayer ? In other words, given raw image data, can we update what is displayed on-screen while the main thread is blocked?

You can't update anything in the view when the main thread is blocked. The whole of UIKit is single-threaded and runs on the main event loop. Video capture is a special case because it draws directly to the screen buffer, but you won't be able to replicate it yourself.

Furthermore, if you do a long-running task on the main thread, iOS will assume your app has crashed and kill it after a few seconds anyway.

Why not perform your other task on a background thread instead? That's the standard practice.

I've found a way to update the UI on non-UI-Thread. We can excute the code in any thread, and it actually changes the layer's transform even when the main thread is sleeping.

self.labelLayer.transform = CATransform3DMakeScale(1.2, 1.2, 1.0);

So if anyone can explain this, please feel free to concat me!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM