简体   繁体   中英

Swift image do not fit in the scrollview

i have a scrollview wich load a 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
}

Help plz

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

This is small comparison of the content modes. 在此处输入图片说明

Just change this.

        imageView.contentMode = .ScaleAspectFit

without zoom

在此处输入图片说明 在此处输入图片说明

with zoom

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
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM