c# – 在Xamarin Forms中获取缓存图像源的原始高度和宽度
发布时间:2020-12-15 07:41:04 所属栏目:百科 来源:网络整理
导读:有没有其他方法可以访问Cached Image的 ImageInformation中的OriginalHeight和OriginalWidth,除了检查成功加载如下? CachedImage img = new CachedImage() { CacheType = FFImageLoading.Cache.CacheType.Memory };img.Source = GetNextImage();img.Success
有没有其他方法可以访问Cached
Image的
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,但如果你有资源文件夹下的图像,也许可以使用下面的方法/想法:
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); } } } 使用w / 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> 然后,您可以创建转换器以设置平板电脑/手机大小,也许是有效的. 我希望这可以帮到你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |