UIImageView从文件系统设置图像

时间:2016-08-26 21:23:56

标签: uiimageview xamarin.ios

我已将图像保存到文件系统,例如路径

file:///private/var/mobile/Containers/Data/Application/B6DDB8AB-0F53-4DFB-A6E7-F94EBBE29F81/tmp/53bea156-8af2-4bdf-b9cc-5f160fa88d01.png

然后我有以下代码来设置UIImageView的图像:

var path = "file:///private/var/mobile/Containers/Data/Application/B6DDB8AB-0F53-4DFB-A6E7-F94EBBE29F81/tmp/53bea156-8af2-4bdf-b9cc-5f160fa88d01.png";
private UIImageView imageView;
imageView.Image = UIImage.FromFile(imageName);

这不起作用,但是......如何在上述路径上为设备保存的文件设置UIImageView的图像?

1 个答案:

答案 0 :(得分:2)

我不知道为什么你需要为你的图标设置一个如此奇怪的路径,有一个正确的样本来设置你的图像:

public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.

        UIImage image1 = UIImage.FromFile ("tips.png");
        UIImage image2 = UIImage.FromFile("Images/tips2.png");

        UIImageView imageView1 = new UIImageView (new CGRect (50, 50, 50, 50));
        imageView1.Layer.BorderColor = UIColor.Black.CGColor;
        imageView1.Layer.BorderWidth = 1;
        imageView1.Image = image1;
        this.Add (imageView1);

        UIImageView imageView2 = new UIImageView (new CGRect (50, 150, 50, 50));
        imageView2.Layer.BorderColor = UIColor.Black.CGColor;
        imageView2.Layer.BorderWidth = 1;
        imageView2.Image = image2;
        this.Add (imageView2);
    }

这是我的解决方案截图:

enter image description here

如果有一些特殊目的使你必须像你发布的代码一样设置一个值,请给我一些细节,我会检查后者。

但可以肯定的是,如果您无法像样本一样获得正确的图像,那么您设置的路径中肯定会出现一些问题。

希望它可以帮到你。