Вопрос

I have a MvvmCross project that uses MvxImageViewLoader to load images from URLs in background. I'm using DefaultImagePath to specify the default image in case the URL is incorrect or no URL has been specified at all.

The issue is that this path is ignoring retina @2x images from the filesystem and always shows the non-retina image.

This is the code:

_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

where ImageView is a standard UIImageView.

But loading the image from path directly works as expected:

// The scale of this image is 2.0 on retina devices (as expected)
var image = UIImage.FromFile(NSBundle.MainBundle.PathForResource("image", "png"));

As a workaround, I added this conditional naming:

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

But this doesn't take into account other naming conventions like ~ipad for iPad and @2x~ipad for iPad retina. So I'm wondering if I'm doing something wrong or it's the MvvmCross plugin that should use the convenient UIImage initialization methods.

It seems that MvxTouchLocalFileImageLoaderclass already uses UIImage.FromFile, so I'm not sure why the behavior is different.

EDIT: Here's a downloadable sample project reproducing the issue.

Thanks.

Это было полезно?

Решение

It seems that adding the prefix res: to the file path makes MvxTouchLocalFileImageLoader to use UIImage.FromFile and otherwise it reads the file to a MemoryStream, ignoring any scale or device qualifier.

So, this now works as expected:

_imageLoader = new MvxImageViewLoader(() => ImageView)
{
    DefaultImagePath = "res:" + NSBundle.MainBundle.PathForResource(imageName, "png")
};

Not sure if this is the intended behavior or it is a bug in MvxTouchLocalFileImageLoader anyway.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top