Example #1
0
        public Task <IBitmap> Load(Stream sourceStream, float?desiredWidth, float?desiredHeight)
        {
            var data = NSData.FromStream(sourceStream);

            var tcs = new TaskCompletionSource <IBitmap>();

#if UIKIT
            NSRunLoop.InvokeInBackground(() =>
            {
                try
                {
                    var bitmap = UIImage.LoadFromData(data);
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image");
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
                }
                catch (Exception ex)
                {
                    LogHost.Default.Debug(ex, "Unable to parse the known colour name.");
                    tcs.TrySetException(ex);
                }
            });
#else
            tcs.TrySetResult(new CocoaBitmap(new UIImage(data)));
#endif
            return(tcs.Task);
        }
Example #2
0
        public Task <IBitmap> LoadFromResource(string source, float?desiredWidth, float?desiredHeight)
        {
            var tcs = new TaskCompletionSource <IBitmap>();

#if UIKIT
            NSRunLoop.InvokeInBackground(() =>
            {
                try
                {
                    var bitmap = UIImage.FromBundle(source);
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image from resource: " + source);
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
                }
                catch (Exception ex)
                {
                    LogHost.Default.Debug(ex, "Unable to parse the known colour name.");
                    tcs.TrySetException(ex);
                }
            });
#else
            NSRunLoop.Main.BeginInvokeOnMainThread(() =>
            {
                try
                {
                    var bitmap = UIImage.ImageNamed(source);
                    if (bitmap == null)
                    {
                        throw new Exception("Failed to load image from resource: " + source);
                    }

                    tcs.TrySetResult(new CocoaBitmap(bitmap));
                }
                catch (Exception ex)
                {
                    tcs.TrySetException(ex);
                }
            });
#endif
            return(tcs.Task);
        }