Centre an image in a scrollview

Discussion in 'iOS Programming' started by boyplunder, Jun 22, 2011.

  1. boyplunder macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #1
    Hi All,

    Haven't been on here recently, but would like a little help in managing an image in a scrollview.

    I have the following code:
    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];
    	self.imageView = tempImageView;
    	[tempImageView release];
    	
    	scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    	scrollView.maximumZoomScale = 1;
    	scrollView.minimumZoomScale = 0.5;
    	scrollView.clipsToBounds = YES;
    	scrollView.delegate = self;
    	[scrollView addSubview:imageView];
    }
    What I'm trying to figure out is a way I can centre the image when the window first appears. IB allows you to offset the image in the view, but this is permanent, which is not what I want. ONce the person moves the image or zooms in or out, it can stay where it is. [I'm feeling this is more complicated than I have done before, but willing to learn, if that's the case.]

    Any help is much appreciated.
     
  2. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #2
    Not really explained too well there.

    The code shown is the initial set up for the view. This works fine. Since then I have been trying to see if I can get the image to start off centred when the user opens the app. Not a huge amount of success so far, and wondered if I could ask for a few suggestions in doing this or the best approach.

    I have learnt through doing and by example, and not a deep programmer like some of you. Any guidance would be appreciated.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Have you checked out UIScrollView's contentOffset property?
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Look at Apple's PhotoScroller sample.

    Centering a view in a scrollview is harder than it should be.
     
  5. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #5
    Thanks Dejo, that was the baby!
    Actually, it led me onto a number of things I will be needing that were not immediately obvious.

    PhoneyDeveloper, I have been working with the example you mentioned, and it has been a great help in starting off.
     
  6. boyplunder thread starter macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
    #6
    Hi All,

    I wanted to post the code that I ended up with after the help I got. There were a couple of other issues that led on from solving the image position problem I posted.

    The issues were: The starting size of the image, the offset position and the compensation in max and min zoom given that I had reduced the logical size of the graphic.

    Code:
    - (void)viewDidLoad {
        [super viewDidLoad];
    	UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.jpg"]];
    	self.imageView = tempImageView;
    	[tempImageView release];
    	
    	scrollView.contentSize = CGSizeMake(imageView.frame.size.width, imageView.frame.size.height);
    	[scrollView setContentOffset:CGPointMake(470.0f, 200.0f)];
    	scrollView.maximumZoomScale = 3;
    	scrollView.minimumZoomScale = 1;
    	scrollView.clipsToBounds = YES;
    	scrollView.delegate = self;
    	[scrollView addSubview:imageView];
    	
    	scrollView.contentSize = CGSizeMake(imageView.image.size.width*0.32, 
    										imageView.image.size.height*0.32 );
    	
    	imageView.frame = CGRectMake(imageView.frame.origin.x, 
    								 imageView.frame.origin.y, 
    								 imageView.frame.size.width*0.32, 
    								 imageView.frame.size.height*0.32);
    	
    }
    This done, I do have a few things to address, but the help was great.
     

Share This Page