当主线程被阻塞时,如何更新 CALayer 的内容?
AVCaptureVideoPreviewLayer 的实例继续即使主线程被阻塞,也会从视频捕获流中更新其内容。是否可以使用 CALayer 的自定义子类来复制此行为?换句话说,给定原始图像数据,我们可以在主线程被阻塞时更新屏幕上显示的内容吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当主线程被阻塞时,您无法更新视图中的任何内容。整个 UIKit 是单线程的,并且在主事件循环上运行。视频捕获是一种特殊情况,因为它直接绘制到屏幕缓冲区,但您无法自己复制它。
此外,如果您在主线程上执行长时间运行的任务,iOS 会假设您的应用程序已崩溃并在几秒钟后终止它。
为什么不在后台线程上执行其他任务呢?这是标准做法。
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.
我找到了一种在非 UI 线程上更新 UI 的方法。
我们可以在任何线程中执行代码,即使主线程处于睡眠状态,它实际上也会改变层的变换。
因此,如果有人可以解释这一点,请随时联系我!
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.
So if anyone can explain this, please feel free to concat me!