在调整视图大小之前,不会显示新的 CALayer.contents
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
displayFrame: 是否在主线程上被调用?如果没有,那么我发现它不会总是绘制。您可以尝试使用 [CATransactionlush] 强制其更新,例如:
如果这不起作用,那么您可以尝试包含图层构造代码以帮助识别该图层的任何特殊性。
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:
If that does not work then you might try including the layer construction code to help identify any peculiarities with this layer.
您可能需要执行
[[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'scontents
might not.使用
[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.