gpt4 book ai didi

ios - 如何为 UIImageView 设置特定大小

转载 作者:行者123 更新时间:2023-11-29 03:28:36 25 4
gpt4 key购买 nike

我遇到了一个奇怪的错误。我正在尝试以编程方式初始化具有特定框架和图像的 UIImageView。我的图像是 60x60,但我试图在屏幕上将其调整为 30x30,这似乎是不可能的。

以下有效,但以 60x60 显示图像:

 UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
// imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];

下面的内容在屏幕上根本不显示任何内容:

  UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
[imgView setContentMode:UIViewContentModeScaleAspectFit];
imgView.frame = CGRectMake(100, 200, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[self.view addSubview:imgView];

怎么了?

编辑:对于那些有同样问题的人,这里是工作代码:

UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];
imgView.frame = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 30, 30);
imgView.center = CGPointMake(pointTouch.x, pointTouch.y);
[imgView setContentMode:UIViewContentModeScaleAspectFill];
[self.view addSubview:imgView];

最佳答案

开始实验

imgView.center = CGPointMake(view.bounds.x/2, view.bounds.y/2);
imgView.bounds = view.bounds;

看看这是否填满了 super View 。然后分别尝试 CGPointMake(pointTouch.x, pointTouch.y)CGPointMake(30,30)

我猜这种行为与 warning 有关关于框架:

If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored.

关于ios - 如何为 UIImageView 设置特定大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com