UIImage.FromData()返回null

时间:2015-02-18 21:01:39

标签: null uiimage touch byte mvvmcross

我正在为我的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 i中有:

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 

1 个答案:

答案 0 :(得分:0)

嗯,这段代码运行正常。这就是它应该如何完成的。

我的问题是将字节传递给包内的视图。我试图通过以下方式实现这一目标:

UTF8Encoding.UTF8.GetString (imageBytes)

和此:

UTF8Encoding.UTF8.GetBytes (navigation.imageBytes);

但是这样,不起作用。我意识到因为字节的长度不一样。所以,我改为这个(现在正在运作):

Convert.ToBase64String (image.Data)

Convert.FromBase64String (navigation.imageBytes)