Hans
Hans

Reputation: 2492

Swift true image height

I have got a UIImageView in a UIScrollView. For this I am trying to set the minimum zoom scale so that the whole picture fits in. However sometimes it is being displayed too small. I have reduced the problem to

self.scrollView.minimumZoomScale = (self.scrollView.frame.height ) / (self.image?.size.height)!

(image being the UIImage inside the view and scrollView being... The UIScrollView)

Sometimes just resulting in the Image having about a third of the height to about double the height. This appears to be totally random, but consistent for each image. I have no idea what is causing it. My best guess is it having something to do with the difference between pixels and points or the @2x and @3x scale factors of iOS devices, but even images taken with the same camera directly after one another sometimes differ.

(Device is iPad Pro)

Upvotes: 2

Views: 54

Answers (1)

nadi9
nadi9

Reputation: 576

Hope this will help

    override func viewDidLoad() {
    super.viewDidLoad()

    let myImageView = UIImageView(image: myImage)
    myImageView.frame.size.width = view.frame.width

    myImageView.contentMode = .ScaleAspectFill

    myView.addSubview(myImageView)

    myScrollView.minimumZoomScale = 1
    myScrollView.maximumZoomScale = 5
}

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {

        return myView
    }   

Upvotes: 1

Related Questions