UIImage.FromData() 返回 null

UIImage.FromData() returning null

我正在为我的 Mvvmcross 应用构建一个插件。对于这个插件,我想要类似的东西:

myPlugin.ShowImage(imageBytes);

然后在我的插件中,我会做:

public void ShowImage(byte[] imageBytes)
    {
        var parameterBundle = new MvxBundle((new MvxDisplayImageViewModel.Nav
        {
            imageBytes = UTF8Encoding.UTF8.GetString (imageBytes)
        }.ToSimplePropertyDictionary()));

        var mvxViewModelRequest = new MvxViewModelRequest (typeof(MvxDisplayImageViewModel), parameterBundle, null, null);
        Mvx.Resolve<IMvxViewDispatcher> ().ShowViewModel (mvxViewModelRequest);
    }

到目前为止,一切正常,但在我的 ViewModel 和 View 中我有:

ViewModel.cs

public class MvxDisplayImageViewModel : MvxViewModel
{
    private byte[] _imageBytes;
    public byte[] ImageBytes 
    {
        get
        { 
            return _imageBytes;
        }
        set
        {
            _imageBytes = value;
            RaisePropertyChanged (() => ImageBytes);
        }
    }

    public class Nav
    {
        public string imageBytes { get; set; }
    }

    public void Init(Nav navigation)
    {
        ImageBytes = UTF8Encoding.UTF8.GetBytes (navigation.imageBytes);
    }

    public MvxDisplayImageViewModel ()
    {
    }
}

查看

public partial class MvxDisplayImageViewController : MvxViewController
{
    protected new MvxDisplayImageViewModel ViewModel
    {
        get { return base.ViewModel as MvxDisplayImageViewModel; }
        set { base.ViewModel = value; }
    }

    public MvxDisplayImageViewController () : base ("MvxDisplayImageViewController", null)
    {
    }

    public override void DidReceiveMemoryWarning ()
    {
        // Releases the view if it doesn't have a superview.
        base.DidReceiveMemoryWarning ();

        // Release any cached data, images, etc that aren't in use.
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        Title = ViewModel.ImageName;

        UIImage image = null;
        try 
        {
            using(var imageNsData = NSData.FromArray(ViewModel.ImageBytes))
            {
                image = new UIImage(imageNsData);
            }
        } 
        catch (Exception e) 
        {
            //image = invalid image...
        }
        finally
        {
            imageView.Image = image;
        }
    }
}

但是,由于某种原因,它没有显示图像:

我在 catch 中收到以下异常:

 Message:
    Could not initialize an instance of the type 'UIKit.UIImage': the native 'initWithData:' method returned nil. It is possible to ignore this condition by setting MonoTouch.ObjCRuntime.Class.ThrowOnInitFailure to false.


Stacktrace:   
    at Foundation.NSObject.InitializeHandle (IntPtr handle, System.String initSelector) [0x000b0] in /Developer/MonoTouch/Source/maccore/src/Foundation/NSObject2.cs:468 
    at UIKit.UIImage..ctor (Foundation.NSData data) [0x00027] in /Developer/MonoTouch/Source/monotouch/src/build/native/UIKit/UIImage.g.cs:108 
    at MyPlugin.Multimedia.Touch.MvxDisplayImageViewController.ViewDidLoad () [0x0002e] in /Users/gbastos/Documents/Gabriel Docs/Development/Plugins/MyPlugin/MyPlugin.Touch/ShowImage/MvxDisplayImageViewController.cs:43 

好吧,这段代码运行良好。就应该这样做。

我的问题是将字节传递给包内的视图时。我正在尝试通过以下方式完成此操作:

UTF8Encoding.UTF8.GetString (imageBytes)

还有这个:

UTF8Encoding.UTF8.GetBytes (navigation.imageBytes);

但是这种方式,行不通。我意识到这是因为字节的长度不一样。所以,我改成了这个(现在可以用):

Convert.ToBase64String (image.Data)

Convert.FromBase64String (navigation.imageBytes)