Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 屏幕截图返回为黑色图像xamarin.ios_C#_Xamarin.forms_Xamarin.ios_Uiimage_Screenshot - Fatal编程技术网

C# 屏幕截图返回为黑色图像xamarin.ios

C# 屏幕截图返回为黑色图像xamarin.ios,c#,xamarin.forms,xamarin.ios,uiimage,screenshot,C#,Xamarin.forms,Xamarin.ios,Uiimage,Screenshot,我正在使用Zxing库生成条形码。我正试图捕获条形码以备将来参考 我已经创建了自定义条形码视图。并为iOS和Android实现了渲染器。但在iOS中,图像是黑色的。我尝试了许多来自互联网的代码,但没有成功 你能告诉我这个密码有什么问题吗 自定义条形码视图 public class BarcodeView : StackLayout { public EventHandler OnDrawBitmap; public static readonly BindablePropert

我正在使用
Zxing
库生成条形码。我正试图捕获条形码以备将来参考

我已经创建了自定义条形码视图。并为iOS和Android实现了渲染器。但在iOS中,图像是黑色的。我尝试了许多来自互联网的代码,但没有成功

你能告诉我这个密码有什么问题吗

自定义条形码视图

public class BarcodeView : StackLayout
{
    public EventHandler OnDrawBitmap;

    public static readonly BindableProperty BarcodeUrlProperty = BindableProperty.Create(nameof(BarcodeUrl),
       typeof(string),
       typeof(string),
       default(string));

    public string BarcodeUrl
    {
        get { return (string)GetValue(BarcodeUrlProperty); }
        set { SetValue(BarcodeUrlProperty, value); }
    }

}
Xaml实现

<custom:BarcodeView Opacity="0" x:Name="barcodeview"><!---->                             
        <zxing:ZXingBarcodeImageView  x:Name="QRCodeView" BarcodeFormat="CODE_93"
                HeightRequest="50" WidthRequest="800" BarcodeValue="{Binding RandomBarcodeNumber}"  VerticalOptions="Center">
               <zxing:ZXingBarcodeImageView.BarcodeOptions>
                    <zxcm:EncodingOptions Width="800" Height="200"/>
               </zxing:ZXingBarcodeImageView.BarcodeOptions>
          </zxing:ZXingBarcodeImageView>                             
</custom:BarcodeView>

iOS渲染器

public class BarcodeViewRenderer : ViewRenderer<BarcodeView, UIView>
{
    public BarcodeViewRenderer()
    {
    }

    BarcodeView view;
    protected override void OnElementChanged(ElementChangedEventArgs<BarcodeView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement != null)
        {
            //register the event
            e.NewElement.OnDrawBitmap += NewElement_OnDrawBitmap;
        }
    }

    private void NewElement_OnDrawBitmap(object sender, EventArgs e)
    {
        try
        {               
            var formsView = Element;
            var rect = new CGRect(formsView.Bounds.X, formsView.Bounds.Y, formsView.Bounds.Width, formsView.Bounds.Height);
            var iOSView = ConvertFormsToNative(formsView, rect);
            UIImage image = ConvertViewToImage(iOSView);

            if (image != null)
                formsView.BarcodeUrl = image.AsPNG().GetBase64EncodedString(Foundation.NSDataBase64EncodingOptions.EndLineWithLineFeed);
            UIGraphics.EndImageContext();
        }
        catch (Exception ex)
        {
        }
    }

    private UIImage ConvertViewToImage(UIView iOSView)
    {
        try
        {
            UIGraphics.BeginImageContextWithOptions(iOSView.Bounds.Size,true, 0);
            iOSView.DrawViewHierarchy(iOSView.Bounds, true);
            var context = UIGraphics.GetCurrentContext();
            iOSView.Layer.RenderInContext(context);
            var img = UIGraphics.GetImageFromCurrentImageContext();

            return img;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    public static UIView ConvertFormsToNative(Xamarin.Forms.View view, CGRect size)
    {
        var renderer = Platform.CreateRenderer(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;
    }
}
公共类BarcodeViewRenderer:ViewRenderer
{
公共条形码视图渲染器()
{
}
条码视图;
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(例如NewElement!=null)
{
//注册活动
e、 NewElement.OnDrawBitmap+=新元素\u OnDrawBitmap;
}
}
私有无效新元素\u OnDrawBitmap(对象发送方,事件参数e)
{
尝试
{               
var formsView=元素;
var rect=new cgrct(formsView.Bounds.X、formsView.Bounds.Y、formsView.Bounds.Width、formsView.Bounds.Height);
var iOSView=ConvertFormsTontial(formsView,rect);
UIImage image=ConvertViewToImage(iOSView);
如果(图像!=null)
formsView.BarcodeUrl=image.AsPNG().GetBase64EncodedString(Foundation.NSDataBase64EncodingOptions.EndLineWithLineFeed);
UIGraphics.EndImageContext();
}
捕获(例外情况除外)
{
}
}
专用UIImage ConvertViewToImage(UIView iOSView)
{
尝试
{
UIGraphics.BeginImageContextWithOptions(iOSView.Bounds.Size,true,0);
DrawViewHierarchy(iOSView.Bounds,true);
var context=UIGraphics.GetCurrentContext();
iOSView.Layer.RenderInContext(context);
var img=UIGraphics.GetImageFromCurrentImageContext();
返回img;
}
捕获(例外情况除外)
{
返回null;
}
}
公共静态UIView ConvertFormsTovative(Xamarin.Forms.View视图,CGRect大小)
{
var renderer=Platform.CreateRenderer(视图);
renderer.NativeView.Frame=大小;
renderer.NativeView.AutoresizingMask=ui视图autoresizing.All;
renderer.NativeView.ContentMode=UIViewContentMode.ScaleToFill;
renderer.Element.Layout(size.ToRectangle());
var nativeView=renderer.nativeView;
nativeView.SetNeedsLayout();
返回本地视图;
}
}

谢谢。

您使用的是哪个版本的库?@MihailDuchev我使用的是中兴库的
2.4.1
。但我认为这不是中兴图书馆的问题。我在ios代码中缺少一些东西