Advertise here

Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UIImageView in UIScrollView

MichafMichaf Posts: 6New Users
edited March 2010 in iPhone SDK Development
Ok, this is something that's been hanging in my code for a while and I just can't get it to work. I just need my UIImageView to get centered in a UIScrollView and zoom working properly. The Problem is the centering, either you set the Frame of your UIImageView to the size of your UIScrollView and the frame being too large the edge of the image bounces off the image, or you set the UIImageView to the frame of the enclosing UIImage but then the UIImageView is placed on the top of the UIScrollView's coordinates. I've tried with .center .contentSize .contentOffSet/inset, the most promizing beeing = The problem is after the zoom the height of the contentSize of the UIScrollView is plain wrong, exceeding the new size of the UIImageView. I've set an xcode project covering this topic at .Mac - iDisk
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIImage* image = [[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Vespa" ofType:@"jpg"]];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
imageView.contentMode = (UIViewContentModeScaleAspectFit);
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor blackColor];
window.backgroundColor = [UIColor blackColor];
// I'm setting the UIImageView size so it fills the screen first
float ratio = image.size.width/[[UIScreen mainScreen] bounds].size.width;
CGRect imageFrame = CGRectMake(0,0, [[UIScreen mainScreen] bounds].size.width, (image.size.height/ratio));
imageView.frame = imageFrame;
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollView.maximumZoomScale = 2.5;
scrollView.minimumZoomScale = 1;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
// =; This is one of my many attempts, try this, the actual size of the scrollView's content view after the zoom gets a wrong height
[image release];
[imageView release];
[window addSubview:scrollView];
    [window makeKeyAndVisible];

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

return imageView;
Post edited by Michaf on


Sign In or Register to comment.