在调整视图大小之前,不会显示新的 CALayer.contents

发布于 2024-11-30 19:51:20 字数 315 浏览 1 评论 0原文

我有一个 CALayer 需要显示视频帧。每次渲染一帧(作为CGImageRef)时,都会调用此方法:

- (void)displayFrame:(CGImageRef)frame {
    [view layer].contents = (id)frame;
}

奇怪的是,它首先不会显示任何内容。每次调整 view 大小时,都会显示一个新的 frame,但会一直保留到再次调整 view 大小为止。

我该如何解决这个问题?

I have a CALayer that needs to display frames of video. Every time a frame is rendered (as a CGImageRef), this method is called:

- (void)displayFrame:(CGImageRef)frame {
    [view layer].contents = (id)frame;
}

What's strange is that this, at first, will not display anything. Every time view is resized, a new frame is displayed, but sticks until view is resized again.

How do I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

孤芳又自赏 2024-12-07 19:51:20

displayFrame: 是否在主线程上被调用?如果没有,那么我发现它不会总是绘制。您可以尝试使用 [CATransactionlush] 强制其更新,例如:

- (void)displayFrame:(CGImageRef)frame {
    [view layer].contents = (id)frame;
    [CATransaction flush];
}

如果这不起作用,那么您可以尝试包含图层构造代码以帮助识别该图层的任何特殊性。

Is displayFrame: being called on the main thread? If not then I have found that it will not always draw. You can try forcing it to update by using [CATransaction flush] like:

- (void)displayFrame:(CGImageRef)frame {
    [view layer].contents = (id)frame;
    [CATransaction flush];
}

If that does not work then you might try including the layer construction code to help identify any peculiarities with this layer.

心病无药医 2024-12-07 19:51:20

您可能需要执行[[view layer] setNeedsDisplay]。需要通知绘图系统已进行更新并且需要重新绘制屏幕。调整视图大小可以做到这一点,但更改图层的内容似乎可能不会。

You probably need to do [[view layer] setNeedsDisplay]. The drawing system needs to be informed that updates have been made and the screen needs to be repainted. Resizing a view does this, but it seems that changing a layer's contents might not.

感情废物 2024-12-07 19:51:20

使用 [CATransactionlush]; 为我解决了这个问题。我必须弄清楚使用这种方法显示文本对性能有何影响(如果有)。

Using [CATransaction flush]; did the trick for me. I have to figure out what (if any) performance hit I take for displaying text using this approach.

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