Xamarin:如何从iOS库项目中加载图像 [英] Xamarin: How to load image from iOS library project

查看:142
本文介绍了Xamarin:如何从iOS库项目中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MvvmCross样式的Xamarin项目.有子项目:

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

  • 核心(PCL)
  • ViewModel(PCL)
  • iOS(可执行文件)

如果我将图像添加到我的iOS项目(Resoureces/Images/test_image.png),则可以使用以下代码加载它:

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

  • 控件(iOS库)

此库应加载图像.我已将图片添加到控件(Resoureces/Images/test_image.png)

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

但是我无法在Controls项目中加载此图像.

But i can not load this image in Controls proj.

我的问题:如何从iOS库加载图像?

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);
        }
    }

并且ViewController类是:

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)); 
        }
    }

这里有完整的项目: https://bitbucket.org/ww_wschaefer/xamarin -first-crossover-app/overview

推荐答案

对我的评论有一些解释.

A little explanation on my comment.

您必须更改

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

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

由于未将图像添加为捆绑资源.

As the image is not added as bundled resource.

UIImage.FromFile()方法异步加载图像. 它还允许应用程序从外部位置加载图像.

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

UIImage.FromFile()方法不同,UIImage.FromBundle()方法是阻止调用,并且仅从应用程序捆绑包内加载图像.但是,它会在加载后缓存图像.

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

这篇关于Xamarin:如何从iOS库项目中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆