透明webview:底层有时不可见
在我基于phonegap的iPhone网络应用程序中,我实现了一个使用AVCaptureVideoPreviewLayer
来拍照的插件。为此,当调用插件的 startCamera
方法时,我将 webview 的背景设置为透明,并将视频捕获层插入到 webview 层下方。这按预期工作(大多数时候)。
但由于某些奇怪的原因,当我第一次执行 startCamera
时(在新的应用程序启动后),视频层不可见。相反,尽管背景颜色设置为 clearColor
,但 Web 视图显示白色背景。对于所有后续执行,视频层都是可见的。
这就是我正在做的显示相机的操作:
AVCaptureSession * session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureVideoPreviewLayer * videoLayer =
[[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
videoLayer.frame = self.webView.bounds;
CALayer *webViewLayer = self.webView.layer;
[webViewLayer.superlayer insertSublayer:videoLayer below:webViewLayer];
// ... session setup excluded
[session startRunning];
[self.webView setBackgroundColor:[UIColor clearColor]];
[self.webView setOpaque:NO];
在 stopCamera()
中,我执行以下操作:
if (session) {
[session stopRunning];
}
[self.webView setBackgroundColor:[UIColor blackColor]];
[self.webView setOpaque:NO];
if (videoLayer != nil) {
[videoLayer removeFromSuperlayer];
}
有什么想法为什么相机层第一次不可见吗?
In my phonegap-based iPhone web-app I implemented a plugin that uses AVCaptureVideoPreviewLayer
to take a photo. To do so, when the plugins' startCamera
method is called, I set the background of the webview to be transparent and insert the video capture layer below the layer of the webview. This works as expected (most of the time).
But for some strange reason, when I execute startCamera
for the first time (after a fresh app start), the video layer isn't visible. Instead, the webview displays a white background, although the background color is set to clearColor
. For all subsequent executions, the video layer is visible.
This is what I'm doing to show the camera:
AVCaptureSession * session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureVideoPreviewLayer * videoLayer =
[[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
videoLayer.frame = self.webView.bounds;
CALayer *webViewLayer = self.webView.layer;
[webViewLayer.superlayer insertSublayer:videoLayer below:webViewLayer];
// ... session setup excluded
[session startRunning];
[self.webView setBackgroundColor:[UIColor clearColor]];
[self.webView setOpaque:NO];
In stopCamera()
I do the following:
if (session) {
[session stopRunning];
}
[self.webView setBackgroundColor:[UIColor blackColor]];
[self.webView setOpaque:NO];
if (videoLayer != nil) {
[videoLayer removeFromSuperlayer];
}
Any ideas why the camera layer isn't visible for the first time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了:问题是,将 webview 的不透明度标志设置为
NO
在startCamera()
中完成时没有任何效果代码>.为了解决这个问题,我提前设置了 webview 的不透明度 - 就在它创建时。没有不透明度的 webview 并不意味着它是透明的 - 您还需要将背景颜色设置为[UIColor clearColor]
(这是在startCamera()
中完成的code>; 在stopCamera()
中,背景颜色设置回[UIColor blackColor]
)。Solved it: The problem was, that setting the opacity flag of the webview to
NO
didn't have any effect when it was done instartCamera()
. To fix it, I set the opacity of the webview earlier - just when it was created. A webview with no opacity doesn't mean that it is transparent though - you also need to set the background color to[UIColor clearColor]
(this is what is done instartCamera()
; instopCamera()
the background color is set back to[UIColor blackColor]
).