在捕获屏幕iOS时在YouTube上获取黑色视频

时间:2018-02-09 17:37:27

标签: ios objective-c xamarin.forms xamarin.ios

我正在使用此代码捕获iOS上的屏幕,但在捕获具有YouTube视频的浏览器时,我获得黑色视频

        var view = UIApplication.SharedApplication.KeyWindow.RootViewController.View;
        UIGraphics.BeginImageContext(view.Frame.Size);
        view.DrawViewHierarchy(view.Frame, true);
        var image = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();

我尝试使用此代码将webview作为参数发送,以便捕获视图刷新并且webveiw始终显示进度环并且从不重新加载:

       public async Task<byte[]> CaptureAsync(Xamarin.Forms.View fView)
    {
        CGRect rect = new CGRect(fView.X, fView.Y, fView.Height, fView.Width);
        var view = ConvertFormsToNative(fView, rect);
        UIGraphics.BeginImageContext(view.Frame.Size);
        view.DrawViewHierarchy(view.Frame, true);
        var image = UIGraphics.GetImageFromCurrentImageContext();
        UIGraphics.EndImageContext();
        using (var imageData = image.AsPNG())
        {
            var bytes = new byte[imageData.Length];
            System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, bytes, 0, Convert.ToInt32(imageData.Length));
            return bytes;
        }
    }
     private UIView ConvertFormsToNative(Xamarin.Forms.View view, CGRect size)
    {

        var renderer = RendererFactory.GetRenderer(view);

        renderer.NativeView.Frame = size;

        renderer.NativeView.AutoresizingMask = UIViewAutoresizing.All;
        renderer.NativeView.ContentMode = UIViewContentMode.ScaleToFill;

        renderer.Element.Layout(size.ToRectangle());

        var nativeView = renderer.NativeView;

        nativeView.SetNeedsLayout();

        return nativeView;
    }

1 个答案:

答案 0 :(得分:0)

这是因为您抓取了RootViewController.View而不是webview或其他内容。

只需确保视图(您要转换为屏幕截图)是视频视图(WebViewWKWebvViewSFSafariViewController.view ..)