我可以只调整 imageView 图像的大小吗?

Can I resize just the image of an imageView?

所以我有这个代码:

override func viewDidLoad() {
        super.viewDidLoad()
        let imageView = UIImageView()
        view.addSubview(imageView)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.layer.borderWidth = 5
        imageView.layer.borderColor = UIColor.green.cgColor
        NSLayoutConstraint.activate([
            imageView.heightAnchor.constraint(equalToConstant: 200),
            imageView.widthAnchor.constraint(equalToConstant: 200),
            imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0),
            imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0),
        ])
        let image = UIImage(named: "3")
        //let image = UIImage(named: "3")?.resizeImageWith(newSize: CGSize(width: 5,height: 5))
        imageView.image = image
    }

看起来像这样:

我正在尝试使用此扩展名仅调整图像大小(因此从绿色边框开始有边距):

func resizeImageWith(newSize: CGSize) -> UIImage {
        let horizontalRatio = newSize.width / size.width
        let verticalRatio = newSize.height / size.height
        let ratio = min(horizontalRatio, verticalRatio)
        let newSize = CGSize(width: size.width * ratio, height: size.height * ratio)
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
        draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: newSize))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage!
    }

但是当我这样使用它时:

let image = UIImage(named: "3")?.resizeImageWith(newSize: CGSize(width: 60,height: 60))

我得到以下结果:

与此相同:

let image = UIImage(named: "3")?.resizeImageWith(newSize: CGSize(width: 5,height: 5))

图像变得最差。 是否可以只调整图像的大小以便与 imageView 有边距?谢谢!

我能够生产出您想要的东西,但摆脱了限制。使用将 imageView 作为子视图的容器视图,您可以像框架一样使用容器并设置其边框宽度和颜色。

框架就位后,您只需设置 imageView 的框架即可根据需要调整图像的大小。代码的最后两行被注释掉了,但是如果你取消注释它们,你会看到图像会缩小以适应设置为 (50, 50) 的新 imageView 帧大小。

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let containerView = UIView()

        containerView.layer.borderWidth = 5
        containerView.layer.borderColor = UIColor.green.cgColor

        containerView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
        containerView.center = view.center

        let imageView = UIImageView()

        view.addSubview(containerView)
        containerView.addSubview(imageView)


        let image = UIImage(named: "3")!
        imageView.frame = CGRect(origin: imageView.frame.origin, size: CGSize(width: 200, height: 200))
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        imageView.center = view.convert(containerView.center, to: containerView)

        print("Image Width: \(image.size.width)")
        print("Image Height: \(image.size.height)")

        print("ImageView Width: \(imageView.frame.width)")
        print("ImageView Height: \(imageView.frame.height)")

        print("Container Width: \(containerView.frame.width)")
        print("Container Height: \(containerView.frame.height)")

        print("View Center: \(view.center.x),\(view.center.y)")
        print("Container Center: \(containerView.center.x),\(containerView.center.y)")

        //imageView.frame.size = CGSize(width: 50, height: 50)
        //imageView.center = view.convert(containerView.center, to: containerView)

    }
}

我使用了 .scaleAspectFit,这样图像将被强制适应您将 imageView 设置为的任何大小,同时保持其原始纵横比。您当然也可以更改它以满足您的需要。如果您需要以不同的方式设置图像的大小,则需要适当地更改 imageView 的 contentMode 属性。更多信息在这里:

https://developer.apple.com/documentation/uikit/uiimageview

convert 函数只是帮助在其父视图中将 imageView 布局到您想要的位置。

打印调试语句有助于检查您的布局,但当然不是必需的。

将图像视图内容模式更改为居中:

imageView.contentMode = .center