Xamarin: How to load image from iOS library project
Asked Answered
N

1

9

I have a Xamarin Project styled with MvvmCross. There are Subprojects:

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

If i add an image to my iOS project (Resoureces/Images/test_image.png), then i can load it with 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 an image. I added an image to Controls (Resoureces/Images/test_image.png)

But i can not load this image in Controls proj.

My Question: How to load 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 is :

    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 full proj: https://bitbucket.org/ww_wschaefer/xamarin-first-crossover-app/overview

Nonplus answered 14/9, 2015 at 13:31 Comment(4)
You cant use from Bundle unless its available as bundeled resource, I think you would be better of using UIImage.FromFile("Images/test_icon.png"). You should also have a look at this Question.Spacial
I can't belive, the solution is so easy, thanks Rohit.Nonplus
glad your problem was solved. I have added the explanation if you are interested.Spacial
If you don't mind please do accept the answer if it solved your problem, so that it would be helpful for others as well. @NonplusSpacial
S
10

A little explanation on my comment.

You have to change

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

to

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

As the image is not added as bundled resource.

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

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

For further understanding have a look at the book - Developing C# Apps for iPhone and iPad using MonoTouch

Spacial answered 14/9, 2015 at 14:21 Comment(1)
@Nonplus - Can you please confirm if this is the solution that worked for you?Spacial

© 2022 - 2024 — McMap. All rights reserved.