So,
I'm trying to do something pretty simple. I have a scrollView that fills the application window. I want to put an imageView in it that can be zoomed and panned.
I have two problems. The first is that I want the image to be initially scaled so as to fit entirely within the scrollView. I have some weird code that does that right now, but I think it's pretty inelegant. It first checks to see whether the image's aspect ratio is bigger or smaller than the application screen's. Then it assigns the appropriate size to the image.
It doesn't work every time. There's got to be a smarter way to do this.
The second problem is that I want to keep the image centered in the scrollView the way images are centered in Apple's photo app. So, if an image's height is smaller than the application window's height but its width is greater than the application window's width, the image remains elastically centered between the top and bottom of the window, although the user can still pan left and right.
All I have right now is a line that sets the ImageView's center to the scrollView's center when it is initialized, but this isn't really a solution.
Any thoughts on these problems?
Thanks!
Charlie
I'm trying to do something pretty simple. I have a scrollView that fills the application window. I want to put an imageView in it that can be zoomed and panned.
I have two problems. The first is that I want the image to be initially scaled so as to fit entirely within the scrollView. I have some weird code that does that right now, but I think it's pretty inelegant. It first checks to see whether the image's aspect ratio is bigger or smaller than the application screen's. Then it assigns the appropriate size to the image.
Code:
if (image.size.width * 1.4375 > image.size.height) {
//width is constraining value
float scaledImageHeight = image.size.height * (scrollView.bounds.size.width / image.size.width);
imageView.frame = CGRectMake(0, 0, scrollView.bounds.size.width, scaledImageHeight);
} else {
//height is constraining value
float scaledImageWidth = image.size.width * (scrollView.bounds.size.height / image.size.height);
imageView.frame = CGRectMake(0, 0, scaledImageWidth, scrollView.bounds.size.height);
}
It doesn't work every time. There's got to be a smarter way to do this.
The second problem is that I want to keep the image centered in the scrollView the way images are centered in Apple's photo app. So, if an image's height is smaller than the application window's height but its width is greater than the application window's width, the image remains elastically centered between the top and bottom of the window, although the user can still pan left and right.
All I have right now is a line that sets the ImageView's center to the scrollView's center when it is initialized, but this isn't really a solution.
Any thoughts on these problems?
Thanks!
Charlie