9

Is there any other way I can access the OriginalHeight and OriginalWidth within the ImageInformation of a CachedImage, other than checking for successful loading as follows?

CachedImage img = new CachedImage() 
{ 
    CacheType = FFImageLoading.Cache.CacheType.Memory 
};
img.Source = GetNextImage();
img.Success += (sender, e) =>
{
    h = e.ImageInformation.OriginalHeight;
    w = e.ImageInformation.OriginalWidth;

    if (Device.Idiom == TargetIdiom.Phone)
    {
        if (h > w)
        {
            img.HeightRequest = 400;
        }
    }
    if (Device.Idiom == TargetIdiom.Tablet)
    {
        if (h > w)
        {
            img.HeightRequest = 800;
        }
     }            
 };
2
  • 1
    Is the image an embedded resource, like a PNG that is in the Resources folder? Or is it a URL that is hosted on the backend? Sep 10, 2018 at 23:56
  • 1
    JPEGs with a build action of Android resource and Bundle resource for ios
    – DarkW1nter
    Sep 11, 2018 at 7:50

1 Answer 1

5
+50

Using FFImageLoading library, your approach is correct w/ Success, but if you have images under Resource folder, maybe can use the approach/idea below:

PLC/Standard:

using Xamarin.Forms;

namespace YourProject.Utils
{
    public interface IImageResource
    {
        Size GetSize(string fileName);
    }
}

Android:

using Android.Graphics;
using YourProject.Droid.Utils;
using YourProject.Utils;
using System;
using Xamarin.Forms;

[assembly: Dependency(typeof(ImageResource))]
namespace YourProject.Droid.Utils
{
    public class ImageResource : Java.Lang.Object, IImageResource
    {
        public Size GetSize(string fileName)
        {
            var options = new BitmapFactory.Options
            {
                InJustDecodeBounds = true
            };

            fileName = fileName.Replace('-', '_').Replace(".png", "").Replace(".jpg", "");
            var resId = Forms.Context.Resources.GetIdentifier(fileName, "drawable", Forms.Context.PackageName);
            BitmapFactory.DecodeResource(Forms.Context.Resources, resId, options);

            return new Size((double)options.OutWidth, (double)options.OutHeight);
        }
    }
}

iOS:

using YourProject.iOS.Utils;
using YourProject.Utils;
using System;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(ImageResource))]
namespace YourProject.iOS.Utils
{
    public class ImageResource : IImageResource
    {
        public Size GetSize(string fileName)
        {
            UIImage image = UIImage.FromFile(fileName);
            return new Size((double)image.Size.Width, (double)image.Size.Height);
        }
    }
}

Usage w/ Xamarin.Forms.DependencyService:

var imageSize = DependencyService.Get<IImageResource>().GetSize("ic_launcher.png");
System.Diagnostics.Debug.WriteLine(imageSize);

Another option via XAML:

<ContentView.Resources>
    <ResourceDictionary>
        <Style x:Key="CachedImageStyle" TargetType="{x:Type ffimageloading:CachedImage}">
           <Setter Property="HorizontalOptions" Value="Center" />
           <Setter Property="VerticalOptions" Value="Center" />
           <Setter Property="HeightRequest">
             <Setter.Value>
               <OnIdiom x:TypeArguments="x:Double" Tablet="800" Phone="400" />
             </Setter.Value>
           </Setter>
         </Style>
    </ResourceDictionary>
</ContentView.Resources>

<ContentView.Content>
    <!-- ... -->
    <ffimageloading:CachedImage
        x:Name="cachedImg"
        Style="{StaticResource CachedImageStyle}">
      </ffimageloading:CachedImage>
    <!-- ... -->
</ContentView.Content>

And then, you can create a converter in order to set the tablet/phone size, maybe works.

I hope this can help you.

1
  • @Jadar in ios image returns null how to resolve it
    – newxamarin
    Oct 19, 2021 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.