3

I have a scrollview which loads an image to zoom in and zoom out, the problem is that I want the image to be loaded full like this

http://www.capital.cl/wp-content/uploads/2015/04/avengers.jpg

so the user can see the complete image first

but it looks like this:

IosEmulator

this is the code

import UIKit

class Paso2: UIViewController, UIScrollViewDelegate {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var noCheckBox2: CheckBox!
    @IBOutlet weak var siCheckBox2: CheckBox!
   
    var imageView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        scrollView.delegate = self
        
        // imageView.contentMode = UIViewContentMode.ScaleAspectFill
        // imageView.clipsToBounds = true
        imageView.image = UIImage(named: "avengers.jpg")
        
        let imagee = UIImage(named: "avengers.jpg")
        let size = imagee?.size
        
        
        imageView.frame = CGRectMake(0, 0, size!.width, size!.height)
        imageView.contentMode = .Top
        scrollView.addSubview(imageView)
        
        scrollView.contentSize = size!
        let scrollViewFrame = scrollView.frame
        let scaleWidth = scrollViewFrame.size.width / scrollView.contentSize.width
        let scaleHeight = scrollViewFrame.size.height / scrollView.contentSize.height
        let minScale = min(scaleHeight, scaleWidth)
        
        scrollView.minimumZoomScale = 1
        scrollView.maximumZoomScale = 4
        scrollView.zoomScale = minScale
        
        centerScrollViewContents()
        
    }
    
    
    
    func centerScrollViewContents(){
        let boundsSize = scrollView.bounds.size
        var contentsFrame = imageView.frame
        
        if contentsFrame.size.width < boundsSize.width {
            contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2
        }
        else {
            contentsFrame.origin.x = 0
        }
        
        if contentsFrame.size.height < boundsSize.height {
            contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2
        }
        else {
            contentsFrame.origin.y = 0
        }
        
        imageView.frame = contentsFrame
        // scrollView.frame = contentsFrame
    }
    
    func scrollViewDidZoom(scrollView: UIScrollView) {
        centerScrollViewContents()
    }
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }

3 Answers 3

4

Just change this.

        imageView.contentMode = .ScaleAspectFit

without zoom

enter image description here enter image description here

with zoom

Sign up to request clarification or add additional context in comments.

Comments

4

You should use .contentMode property of the UIImageView for the proper representation of the image.

This is small comparison of the content modes. enter image description here

Comments

0

You want to display full image without scrolling or you want to display scrollable image?

Try this code for fast start:

class ViewController: UIViewController {

@IBOutlet weak var scrollView: UIScrollView!
var imageView = UIImageView()

private var imageViewOriginalSize = CGRect ()

override func viewDidLoad() {
    super.viewDidLoad()


    imageView.image = UIImage(named: "avengers.jpg")
    imageView.contentMode = .ScaleAspectFit
    let size = UIScreen.mainScreen().bounds
    imageViewOriginalSize = size
    imageView.frame = CGRectMake(0, 0, size.width, size.height)
    scrollView.addSubview(imageView)

    let pinch = UIPinchGestureRecognizer(target: self, action: #selector(ViewController.pinchGestureRecognizerAction))
    self.scrollView.addGestureRecognizer(pinch)

}

func pinchGestureRecognizerAction(gestureRecoginzer: UIPinchGestureRecognizer) {
    imageView.frame.size.height = imageViewOriginalSize.height*gestureRecoginzer.scale
    imageView.frame.size.width = imageViewOriginalSize.width*gestureRecoginzer.scale
    imageView.frame.origin.x = imageViewOriginalSize.origin.x*gestureRecoginzer.scale
    imageView.frame.origin.y = imageViewOriginalSize.origin.y*gestureRecoginzer.scale
    scrollView.contentSize = imageView.frame.size
}}

2 Comments

i want the user to see the full image first and from that begin to zoom in or zoom out
I found this: github.com/dblock/ARTiledImageView May be you should use this pod?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.