Xamarin: How to download an image from an iOS library project

I have a Xamarin project in the style of MvvmCross. There are subprojects:

  • Core (PCL)
  • ViewModel (PCL)
  • iOS (Executable)

If I add an image to my iOS project (Resoureces / Images / test_image.png), I can upload it using this code:

UIImage image = UIImage.FromBundle("Images/test_icon.png"); 

Now I want to use a new subproject

  • Controls (iOS Library)

This library should load the image. I added an image to the controls (Resoureces / Images / test_image.png)

But I can’t upload this image to Controls proj.

My question is: how to download images from iOS libraries?

  public class MyButton : UIButton { public MyButton () : base() { Initialize (); } void Initialize() { // load image from bundle UIImage image = UIImage.FromBundle("Images/test_icon.png"); // image is null this.SetImage (image, UIControlState.Normal); } } 

and the ViewController class:

  public partial class FirstView : MvxViewController { public FirstView () : base ("FirstView", null) { } public override void ViewDidLoad () { base.ViewDidLoad (); // load image from bundle // UIImage image = UIImage.FromBundle("Images/test_icon.png"); // image is not null if added in iOS Proj // this.imageView.Image = image; MyButton button = new MyButton (); View.Add (button); View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Right, NSLayoutRelation.Equal, View, NSLayoutAttribute.Right, 1, 10)); View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Top, NSLayoutRelation.Equal, View, NSLayoutAttribute.Top, 1, 74)); View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, 64)); View.AddConstraint (NSLayoutConstraint.Create (button, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 0, 64)); } } 

enter image description here

Here is the complete project: https://bitbucket.org/ww_wschaefer/xamarin-first-crossover-app/overview

+6
source share
1 answer

A little explanation of my comment.

You have to change

 UIImage image = UIImage.FromBundle("Images/test_icon.png"); 

to

 UIImage image = UIImage.FromFile("Images/test_icon.png"); 

Because the image is not added as a related resource.

The UIImage.FromFile() method loads the image asynchronously. It also allows the application to download an image from an external location .

Unlike the UIImage.FromFile() method, the UIImage.FromFile() method is a blocking call, and only loads images from the application set . However, it caches images after loading.

For further understanding, take a look at the book - C # Application Development for iPhone and iPad Using MonoTouch

+8
source

All Articles