当主线程被阻塞时,如何更新 CALayer 的内容?

发布于 2025-01-02 11:23:21 字数 294 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薆情海 2025-01-09 11:23:21

当主线程被阻塞时,您无法更新视图中的任何内容。整个 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.

是伱的 2025-01-09 11:23:21

我找到了一种在非 UI 线程上更新 UI 的方法。
我们可以在任何线程中执行代码,即使主线程处于睡眠状态,它实际上也会改变层的变换。

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

因此,如果有人可以解释这一点,请随时联系我!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文