透明webview:底层有时不可见

发布于 2024-12-20 06:02:43 字数 1208 浏览 2 评论 0原文

在我基于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 技术交流群。

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

发布评论

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

评论(1

请你别敷衍 2024-12-27 06:02:43

解决了:问题是,将 webview 的不透明度标志设置为 NOstartCamera() 中完成时没有任何效果代码>.为了解决这个问题,我提前设置了 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 in startCamera(). 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 in startCamera(); in stopCamera() the background color is set back to [UIColor blackColor]).

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