ios - MvxImageViewLoader 默认图像忽略视网膜资源

标签 ios xamarin.ios uiimageview mvvmcross retina-display

我有一个 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 初始化方法。

似乎MvxTouchLocalFileImageLoaderalready 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/

相关文章:

uitableview - MonoTouch 如何为带有静态单元格的 UITableViewController 实现 RowSelected

c# - 如何在 Xamarin 中从我的 iOS 应用程序打开 Facebook/Twitter 应用程序?

datetime - 如何使用 MonoTouch 将 DateTime 格式化为短时间格式的本地用户区域设置

iphone - 如何从 UIImagePickerController 获取缩略图或可保存路径以用于 UIImageView?

ios - 如何获取 ImageView 中图像的实际高度以进行纵横比匹配

iphone - 如何给图像赋值

iphone - 如何编程 UITextField 的键盘以在 viewDidLoad 上打开?

ios - 如何在 UIActivityViewController 模式中显示文件大小和类型

ios - 填充 UISearchBarController 结果 TableView 时遇到问题

iphone - 如何实现像通知中心的股票行情自动 ScrollView ?