我有一个 MvvmCross 项目,它使用 MvxImageViewLoader
在后台从 URL 加载图像。我正在使用 DefaultImagePath
指定默认图像,以防 URL 不正确或根本没有指定 URL。
问题在于此路径忽略文件系统中的视网膜 @2x
图像并始终显示非视网膜图像。
这是代码:
_imageLoader = new MvxImageViewLoader(() => ImageView)
{
// After loading the Image view, the scale of the image is always 1.0
DefaultImagePath = NSBundle.MainBundle.PathForResource("image", "png")
};
// At this point, ImageView.Image.CurrentScale is 1.0 (WRONG) on retina devices
其中 ImageView
是标准的 UIImageView
。
但是直接从路径加载图像按预期工作:
// The scale of this image is 2.0 on retina devices (as expected)
var image = UIImage.FromFile(NSBundle.MainBundle.PathForResource("image", "png"));
作为解决方法,我添加了这个条件命名:
var imageName = UIScreen.MainScreen.Scale > 1 ? "image@2x" : "image";
_imageLoader = new MvxImageViewLoader(() => ImageView)
{
DefaultImagePath = NSBundle.MainBundle.PathForResource(imageName, "png")
};
// At this point, ImageView.Image.CurrentScale is 2.0 on retina devices as expected
但这并没有考虑其他命名约定,例如 iPad 的 ~ipad
和 iPad retina 的 @2x~ipad
。所以我想知道是我做错了什么,还是 MvvmCross 插件应该使用方便的 UIImage
初始化方法。
似乎MvxTouchLocalFileImageLoader
类already uses UIImage.FromFile
,所以我不确定行为为何不同。
编辑:这是一个downloadable sample project重现问题。
谢谢。
最佳答案
似乎adding the prefix res:
to the file path制作 MvxTouchLocalFileImageLoader
to use UIImage.FromFile
否则 it reads the file to a MemoryStream
, 忽略任何比例或设备限定符。
所以,这现在按预期工作:
_imageLoader = new MvxImageViewLoader(() => ImageView)
{
DefaultImagePath = "res:" + NSBundle.MainBundle.PathForResource(imageName, "png")
};
不确定这是预期的行为还是 MvxTouchLocalFileImageLoader
中的错误。
关于ios - MvxImageViewLoader 默认图像忽略视网膜资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21241632/