0

enter image description here

I have a UIImageView inside a scrollView. Its initial width and height are equal to 100. I want this UIImageView to appear in same size even after zooming. For that I modify its size inside scrollViewDidEndZooming method. This is working fine without a problem. However UiImage of the UiImageView looks blurry when zoom scale is increased. I tried to set contentScale property of the UiImageView, but it is not working.

How I can fix this?

import UIKit

class ViewController: UIViewController {
    
    static var zoomScale: CGFloat = 1.0
    
    let scrollView = UIScrollView()
    let contentView = UIView()
    let imageView: UIImageView = UIImageView()
    let imageViewSize: CGFloat = 100
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .yellow
        
        self.setupScrollView()
        self.setuContentView()
        self.addImageView()
        
    }
    
    func setupScrollView() {
        let vWidth = self.view.frame.width
        let vHeight = self.view.frame.height
        
        scrollView.delegate = self
        scrollView.frame = CGRect(x: 0, y: 0, width: vWidth, height: vHeight)
        scrollView.backgroundColor = .lightGray
        scrollView.alwaysBounceVertical = false
        scrollView.alwaysBounceHorizontal = false
        scrollView.showsVerticalScrollIndicator = true
        scrollView.flashScrollIndicators()
        
        scrollView.minimumZoomScale = 1.0
        scrollView.maximumZoomScale = 10.0
        
        self.view.addSubview(scrollView)
    }
    
    func setuContentView() {
        contentView.frame = scrollView.frame.insetBy(dx: 20, dy: 20)
        contentView.backgroundColor = .white
        scrollView.addSubview(contentView)
    }
    
    func addImageView(){
        let image = UIImage(systemName: "arrow.triangle.2.circlepath")
        
        imageView.frame = CGRect(x: 200, y: 200, width: imageViewSize, height: imageViewSize)
        imageView.image = image
        imageView.contentMode = .scaleAspectFit
        imageView.backgroundColor = .systemBlue
        imageView.tintColor = .white
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = 0.5 * imageView.bounds.size.width // I want rounded imageView
        self.contentView.addSubview(imageView)
    }

}

extension ViewController: UIScrollViewDelegate {
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.contentView
    }
   
    
    func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
        ViewController.zoomScale = scale
        
        imageView.frame = CGRect(x: imageView.frame.origin.x, y: imageView.frame.origin.y, width: imageViewSize/scale, height: imageViewSize/scale)
        imageView.layer.cornerRadius = 0.5 * imageView.bounds.size.width
        imageView.layer.contentsScale = scale
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
S.Basnagoda
  • 671
  • 2
  • 8
  • 21
  • I guess the problem is in the resolution of the image you put into `UIImageView`. Try any custom image instead of `UIImage(systemName: "arrow.triangle.2.circlepath")`. Provide e.g. an image 100x100 pixels, and 30x30 pixels, and see if 30x30 will be more blurred at the max. scale – olha Feb 17 '21 at 09:34
  • Does this answer your question? "[Views drawn from code (PaintCode) are pixelated, very pixelated when scaled](https://stackoverflow.com/a/43003179/90527)" – outis Sep 09 '21 at 20:01

0 Answers0