Change image scale in UIImageView

67 Views Asked by At

I'm trying to change the image scale in a UIImageView, I want it to be smaller and keep its ratio.

I tried:

cameraPhoto.image?.scale = CGFloat(0.5)

but it doesn't work. I get:

Cannot assign to property: 'scale' is a get-only property

I want this:

enter image description here

instead of this:

enter image description here

How can I reduce the camera picture size (like on the example above)?

2

There are 2 best solutions below

0
AntiVIRUZ On

I think you need to create UIImageView with size of your image and place it on UIScrollView (and disable scroll). Then you can change scale of scroll view. https://www.brightec.co.uk/blog/creating-a-zoomable-image-view-in-swift

0
DonMag On

Very quick example of embedding the image view in a 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
    }
    
}

Looks like this (size 60 x 60):

enter image description here

and an example View Controller, overlaying it on a label:

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),
            
        ])
        
    }
    
}

enter image description here