更改 UIImageView 中的图像比例

问题描述 投票:0回答:3

我正在尝试更改 UIImageView 中的图像比例,我希望它变小并保持其比例。

我尝试过:

cameraPhoto.image?.scale = CGFloat(0.5)

但它不起作用。我得到:

无法分配给属性:“scale”是仅获取属性

我想要这个:

而不是这个:

如何缩小相机图片尺寸(如上例所示)?

ios swift uiimageview uiimage
3个回答
1
投票

将图像视图嵌入到

UIView
中的非常快速的示例:

class CameraView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }
    private func commonInit() {
        backgroundColor = .white
        // if we want thin, bold, regular, etc
        let imgWeight = UIImage.SymbolConfiguration(weight: .bold)
        // safely unwrap optional system image named "camera"
        if let img = UIImage(systemName: "camera", withConfiguration: imgWeight) {
            // create image view
            let imgView = UIImageView(image: img)
            // if we want a specific color
            imgView.tintColor = .black
            // we want aspect fit
            imgView.contentMode = .scaleAspectFit
            imgView.translatesAutoresizingMaskIntoConstraints = false
            addSubview(imgView)
            NSLayoutConstraint.activate([
                // center the image view
                imgView.centerXAnchor.constraint(equalTo: centerXAnchor),
                imgView.centerYAnchor.constraint(equalTo: centerYAnchor),
                // let's make it 60% of the size of this view
                imgView.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.6),
                imgView.heightAnchor.constraint(equalTo: imgView.widthAnchor),
            ])
        }
        // add shadow to self
        layer.shadowOffset = .zero
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowRadius = 3
        layer.shadowOpacity = 0.4
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        // make self a circle
        //  assumes this view has a 1:1 size ratio
        layer.cornerRadius = bounds.width * 0.5
    }
    
}

看起来像这样(尺寸 60 x 60):

和一个示例视图控制器,将其覆盖在标签上:

class CameraViewTestVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .systemBackground
        
        let label = UILabel()
        label.backgroundColor = .cyan
        label.numberOfLines = 0
        label.textAlignment = .center
        label.text = "Sample\nLabel"
        label.font = .systemFont(ofSize: 40.0)
        
        let camView = CameraView()
        
        label.translatesAutoresizingMaskIntoConstraints = false
        camView.translatesAutoresizingMaskIntoConstraints = false
        
        view.addSubview(label)
        view.addSubview(camView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
        
            label.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            label.centerYAnchor.constraint(equalTo: g.centerYAnchor),
            
            camView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: -30.0),
            camView.leadingAnchor.constraint(equalTo: label.trailingAnchor, constant: -30.0),
            camView.widthAnchor.constraint(equalToConstant: 60.0),
            camView.heightAnchor.constraint(equalTo: camView.widthAnchor, multiplier: 1.0),
            
        ])
        
    }
    
}


0
投票

您可以从此扩展中使用:

extension UIImage {
    func imageWithSize(size: CGSize) -> UIImage? {  
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale);
        let rect = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height);
        draw(in: rect)
        
        let resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return resultingImage
    }


    func imageWithScale(scale: CGFloat) -> UIImage? {
        let width = self.size.width * scale
        let height = self.size.height * scale
        return self.imageWithSize(size: CGSize(width: width, height: height))
    }
}

并在您的图像中使用此功能,例如:

let img = yourImage?.imageWithScale(scale: 0.5)

您还应该使用 UIImageView 属性:.contentMode,例如:

imageView?.contentMode = .scaleAspectFill //or what ever you want(need)

如果您想要图像中有额外的边距,您可以在 UIImage 扩展中添加此功能:

func imageWithSize(size: CGSize, extraMargin: CGFloat) -> UIImage? {
        
        let imageSize = CGSize(width: size.width + extraMargin * 2, height: size.height + extraMargin * 2)
        
        UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale);
        let drawingRect = CGRect(x: extraMargin, y: extraMargin, width: size.width, height: size.height)
        draw(in: drawingRect)
        
        let resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        return resultingImage
    }

-1
投票

我认为您需要创建具有图像大小的 UIImageView 并将其放置在 UIScrollView 上(并禁用滚动)。然后您可以更改滚动视图的比例。 https://www.brightec.co.uk/blog/creating-a-zoomable-image-view-in-swift

© www.soinside.com 2019 - 2024. All rights reserved.