Hi,
part of my app is an image viewer of postprocessed images shot with the iPhone. I would like to display the images in aspect fit in the center of the screen inside a ScrollView. To do so I made a UIScrolView and UIImageView with constraints to fill the whole screen. The UIImageView is set to Aspect Fill, whereas the UIScrollView is set to Aspect Fit in the Storyboard.
When the result ViewController is called, the image still needs to be calculated, so I show a ActivityIndicator. When I just call
self.imageView.image = self.result_image
after the image is processed, the image fits from its height in portrait mode, but with regarding to its width, you can see therefore rafly 30% of the image. The center if that matters. To overcome that I run the following code directly after I asign an image to the imageView (the line above):
self.scrollView.contentMode = .ScaleAspectFit
self.imageView.sizeToFit()
self.scrollView.contentSize = CGSizeMake(self.imageView.frame.size.width, self.imageView.frame.size.height)
let imageViewSize = self.imageView.bounds.size
let scrollViewSize = self.scrollView.bounds.size
let widthScale = scrollViewSize.width / imageViewSize.width
let heightScale = scrollViewSize.height / imageViewSize.height
let minZoomScale = min(widthScale, heightScale)
self.scrollView.minimumZoomScale = minZoomScale
self.scrollView.zoomScale = minZoomScale
Which displays the whole image in Aspect Fit at the upper border of the screen.
Once I zoom, however, the image get repositioned and resized:
The only other function regarding any of these two elements in my code is:
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return self.imageView
}
Can anyone explain this strange behaviour and has any suggestions what I can do? I thought actually this would be an easy problem...
I really appreciate your help
Best,
Chris
Hi QuinceyMorris,
Thank you so much for your help and the time you invested, but I couldn't make to work like that. I used now the following github example and now its working:
https://github.com/evgenyneu/ios-imagescroll-swift
I think my problem was that I did not set the ImageView in the Storyboard to be a Placeholder or something like that. At least that was one difference between my project file and theirs.
Thanks again a lot for your help.
Best,
Chris