c# - 在 Xamarin Forms 中获取缓存图像源的原始高度和宽度

标签 c# image xamarin.forms

除了按如下方式检查加载是否成功之外,还有其他方法可以访问 CachedImage 的 ImageInformation 中的 OriginalHeight 和 OriginalWidth 吗?

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;
        }
     }            
 };

最佳答案

使用 FFImageLoading 库,你的方法是正确的 w/Success,但是如果你的 Resource 文件夹下有图像,也许可以使用下面的方法/想法:

PLC/标准:

using Xamarin.Forms;

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

安卓:

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);
        }
    }
}

使用 Xamarin.Forms.DependencyService:

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

通过 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>

然后,您可以创建一个转换器来设置平板电脑/手机的尺寸,也许可行。

希望对您有所帮助。

关于c# - 在 Xamarin Forms 中获取缓存图像源的原始高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52225049/

相关文章:

c# - WireMock.Net 如何响应有时错误和其他 OK

c# - 如何获取 DbSet 的主键?

java - 打印图像中的行分隔符

c# - 如何使用 ScrollTo 滚动到 CollectionView 中开始?

c# - 使用 webjobs 调用另一个网站的 api

javascript - 如何在 slickgrid 中显示行号列?

c# - 图像文件副本,正在被另一个进程使用

javascript - 在html5 Canvas 上逐帧绘制视频并用 slider 控制

ios - CarouselView.FormsPlugin 未在 iOS 上显示

ios - xamarin 表单 ios 版本构建挂起