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

前端之家收集整理的这篇文章主要介绍了c# – 在Xamarin Forms中获取缓存图像源的原始高度和宽度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有其他方法可以访问Cached ImageImageInformation中的OriginalHeight和OriginalWidth,除了检查成功加载如下?
  1. CachedImage img = new CachedImage()
  2. {
  3. CacheType = FFImageLoading.Cache.CacheType.Memory
  4. };
  5. img.Source = GetNextImage();
  6. img.Success += (sender,e) =>
  7. {
  8. h = e.ImageInformation.OriginalHeight;
  9. w = e.ImageInformation.OriginalWidth;
  10.  
  11. if (Device.Idiom == TargetIdiom.Phone)
  12. {
  13. if (h > w)
  14. {
  15. img.HeightRequest = 400;
  16. }
  17. }
  18. if (Device.Idiom == TargetIdiom.Tablet)
  19. {
  20. if (h > w)
  21. {
  22. img.HeightRequest = 800;
  23. }
  24. }
  25. };

解决方法

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

PLC /标准:

  1. using Xamarin.Forms;
  2.  
  3. namespace YourProject.Utils
  4. {
  5. public interface IImageResource
  6. {
  7. Size GetSize(string fileName);
  8. }
  9. }

安卓:

  1. using Android.Graphics;
  2. using YourProject.Droid.Utils;
  3. using YourProject.Utils;
  4. using System;
  5. using Xamarin.Forms;
  6.  
  7. [assembly: Dependency(typeof(ImageResource))]
  8. namespace YourProject.Droid.Utils
  9. {
  10. public class ImageResource : Java.Lang.Object,IImageResource
  11. {
  12. public Size GetSize(string fileName)
  13. {
  14. var options = new BitmapFactory.Options
  15. {
  16. InJustDecodeBounds = true
  17. };
  18.  
  19. fileName = fileName.Replace('-','_').Replace(".png","").Replace(".jpg","");
  20. var resId = Forms.Context.Resources.GetIdentifier(fileName,"drawable",Forms.Context.PackageName);
  21. BitmapFactory.DecodeResource(Forms.Context.Resources,resId,options);
  22.  
  23. return new Size((double)options.OutWidth,(double)options.OutHeight);
  24. }
  25. }
  26. }

iOS版:

  1. using YourProject.iOS.Utils;
  2. using YourProject.Utils;
  3. using System;
  4. using UIKit;
  5. using Xamarin.Forms;
  6.  
  7. [assembly: Dependency(typeof(ImageResource))]
  8. namespace YourProject.iOS.Utils
  9. {
  10. public class ImageResource : IImageResource
  11. {
  12. public Size GetSize(string fileName)
  13. {
  14. UIImage image = UIImage.FromFile(fileName);
  15. return new Size((double)image.Size.Width,(double)image.Size.Height);
  16. }
  17. }
  18. }

使用w / Xamarin.Forms.DependencyService:

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

XAML的另一个选择:

  1. <ContentView.Resources>
  2. <ResourceDictionary>
  3. <Style x:Key="CachedImageStyle" TargetType="{x:Type ffimageloading:CachedImage}">
  4. <Setter Property="HorizontalOptions" Value="Center" />
  5. <Setter Property="VerticalOptions" Value="Center" />
  6. <Setter Property="HeightRequest">
  7. <Setter.Value>
  8. <OnIdiom x:TypeArguments="x:Double" Tablet="800" Phone="400" />
  9. </Setter.Value>
  10. </Setter>
  11. </Style>
  12. </ResourceDictionary>
  13. </ContentView.Resources>
  14.  
  15. <ContentView.Content>
  16. <!-- ... -->
  17. <ffimageloading:CachedImage
  18. x:Name="cachedImg"
  19. Style="{StaticResource CachedImageStyle}">
  20. </ffimageloading:CachedImage>
  21. <!-- ... -->
  22. </ContentView.Content>

然后,您可以创建转换器以设置平板电脑/手机大小,也许是有效的.

我希望这可以帮到你.

猜你在找的C#相关文章