Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iOS使图像适合屏幕_C#_Ios_Image_User Interface_Xamarin - Fatal编程技术网

C# iOS使图像适合屏幕

C# iOS使图像适合屏幕,c#,ios,image,user-interface,xamarin,C#,Ios,Image,User Interface,Xamarin,我有一个646x289的图像,我正试图在屏幕上显示它的纵横比 以下是我目前的做法: 控制器: public override void ViewDidLayoutSubviews() { base.ViewDidLayoutSubviews(); _imnLogo.ContentMode = UIViewContentMode.ScaleAspectFit; _imnLogo.SizeToFit(); _imnLogo.Frame = new CGRect(

我有一个646x289的图像,我正试图在屏幕上显示它的纵横比

以下是我目前的做法:

控制器:

public override void ViewDidLayoutSubviews()
{
    base.ViewDidLayoutSubviews();
    _imnLogo.ContentMode = UIViewContentMode.ScaleAspectFit;
    _imnLogo.SizeToFit();
    _imnLogo.Frame = new CGRect(
        View.Bounds.Left + 2 * Globals.MarginGrid,
        View.Bounds.Top + Globals.MarginGrid,
        _scaledImage.Size.Width, _scaledImage.Size.Height);
}

public override void LoadView()
{
    base.LoadView();
    _scaledImage = MaxResizeImage(
        UIImage.FromFile("imn_logo.png"), (float) View.Bounds.Width, (float) View.Bounds.Height);

    _imnLogo = new UIImageView(_scaledImage);
    View.AddSubview(_imnLogo);
}

public UIImage MaxResizeImage(UIImage sourceImage, float maxWidth, float maxHeight)
{
    var sourceSize = sourceImage.Size;
    var maxResizeFactor = Math.Max(maxWidth / sourceSize.Width, maxHeight / sourceSize.Height);
    if (maxResizeFactor > 1) return sourceImage;

    var width = maxResizeFactor * sourceSize.Width;
    var height = maxResizeFactor * sourceSize.Height;
    UIGraphics.BeginImageContext(new SizeF((float) width, (float) height));
    sourceImage.Draw(new RectangleF(0, 0, (float) width, (float) height));
    var resultImage = UIGraphics.GetImageFromCurrentImageContext();
    UIGraphics.EndImageContext();
    return resultImage;
}
加载时,图像太大,无法显示在屏幕中


我也在用C#(使用Xamarin)构建我的所有接口,因此我需要能够使用帧和边界来实现这一点。

使用
UIImageView的
ContentMode
来控制图像的缩放方式,您可以跳过手动调整大小:

public override void LoadView()
{
    base.LoadView();
    _imnLogo = new UIImageView(UIImage.FromFile("imn_logo.png"));
    _imnLogo.Frame = View.Frame;
    _imnLogo.ContentMode = UIViewContentMode.ScaleAspectFill;
    View.AddSubview(imageView);
    View.SendSubviewToBack(imnLogo); // Do this if you want to place other `View`s on top of the logo...
}

Ref:

这就是我想要的答案,非常感谢您,先生@kformeck np,很高兴它有帮助。。。然后自己调整大小非常容易,iOS会加速并缓存它。。。。。