从使用嵌入式HTML和WKWebView渲染的Swift视频中删除白色背景/边框。

huangapple go评论93阅读模式
英文:

remove white background/border from swift video rendered using embedded HTML and WKWebView

问题

你好,我有一个YouTube结构,用于加载YouTube短视频,但视频周围有大白色背景或边框,我无法去掉它。我尝试了应用.tint、.foreground、.background等属性,但都没有效果。唯一改变白色边框大小的方法是修改嵌入的HTML中的宽度和高度。似乎视频没有填充整个框架,所以白色边框填充了其余部分。如果有人知道我如何修改这段代码来去掉白色边框,我将不胜感激。我还面临一个问题,YouTube的播放/暂停按钮以及时间条和所有其他视频选项都很小,不随视频大小增加而增加。

import SwiftUI
import WebKit

struct SwiftUIView: View {
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        VStack{
            YouTubeView(link: "-rHELEJuqkU")               
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

struct YouTubeView: UIViewRepresentable {
    let link: String

    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        return WKWebView(frame: .zero, configuration: webConfiguration)
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
     
        let embedHTML = """
            <div style="display: flex; justify-content: center; align-items: center; height: 100vh;">
                <iframe width="500" height="920" src="https://www.youtube.com/embed/\(link)" frameborder="0" allowfullscreen></iframe>
            </div>
        """
        uiView.scrollView.isScrollEnabled = false
        uiView.loadHTMLString(embedHTML, baseURL: nil)
    }
}

请注意,我已将代码中的"替换为正常的引号(")。希望这可以解决你的问题。

英文:

Hello I have a YouTube struct that loads in YouTube shorts and the videos have a large white background or border around them and I can't get it to go away. I tried applying .tint, .foreground, .background and none work. The only way of changing the size of the white border is by changing the width and height inside the embedded HTML. It seems like the video doesn't fill the whole frame so a white border fills the rest. If anyone knows how I can modify this code to get ride of the white border Id appreciate it. I am also facing an issue where the YouTube play/pause button along with the time bar and all the other video options are very small and don't increase with the size of the video.

import SwiftUI
import WebKit

struct SwiftUIView: View {
    @Environment(\.colorScheme) var colorScheme
    var body: some View {
        VStack{
            YouTubeView(link: &quot;-rHELEJuqkU&quot;)               
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView()
    }
}

struct YouTubeView: UIViewRepresentable {
    let link: String

    func makeUIView(context: Context) -&gt; WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        return WKWebView(frame: .zero, configuration: webConfiguration)
    }
    
    func updateUIView(_ uiView: WKWebView, context: Context) {
     
        let embedHTML = &quot;&quot;&quot;
                &lt;div style=&quot;display: flex; justify-content: center; align-items: center; height: 100vh;&quot;&gt;
                    &lt;iframe width=&quot;500&quot; height=&quot;920&quot; src=&quot;https://www.youtube.com/embed/\(link)&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;
                &lt;/div&gt;
            &quot;&quot;&quot;
        uiView.scrollView.isScrollEnabled = false
        uiView.loadHTMLString(embedHTML, baseURL: nil)
    }
}

答案1

得分: 1

因为很难指定YouTube控件的大小,你可以像这样将整个视图放大:

YouTubeView(link: "-rHELEJuqkU")
   .scaleEffect(3, anchor: .topLeading)

然后相应地调整你的HTML代码:

let embedHTML = """
<body style="background-color:black;">
    <iframe
    width="315"
    height="560"
    src="https://www.youtube.com/embed/\(link)" frameborder="0" allowfullscreen></iframe>
</body>
"""
它不是像素完美的,因为视频的大小和纵横比将不匹配iOS设备的大小和纵横比,但它非常接近。
英文:

since it's hard to specify the size of the youtube controls, you could scale the entire view up like this

YouTubeView(link: &quot;-rHELEJuqkU&quot;)
   .scaleEffect(3, anchor: .topLeading)

then adjust your html code accordingly

let embedHTML = &quot;&quot;&quot;
&lt;body style=&quot;background-color:black;&quot;&gt;
    &lt;iframe
    width=&quot;315&quot;
    height=&quot;560&quot;
    src=&quot;https://www.youtube.com/embed/\(link)&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;
&lt;/body&gt;
&quot;&quot;&quot;

it's not pixel perfect since the size and aspect ratio of the video will not match the size and aspect ratio of the iOS device.. but it's pretty close.

huangapple
  • 本文由 发表于 2023年5月30日 05:02:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定