2

I want to display a photo in a scroll view. Because I want to display the whole photo to the user and then allow user to pinch the photo to zoom in and out. So firstly I set the contentsize to a fixed size(such as (320, 568)). However, when I add a UIImageView as the subview of the the scroll view, if the image of the UIImageView's size is larger than the contentsize of the scroll view, only the upper and left part of the image will be displayed. So I want to know how to restrict the imageview in the scrollview and set image displayed well(like the photo in most apps). I know a method: clipsToBounds, and I use it. But it dose not work. Can anyone help me? Here is my simple code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.photoScrollView.contentSize = CGSizeMake(320, 568);
    self.photoScrollView.clipsToBounds = YES;

    UIImage *image = [UIImage imageNamed:@"image"];
    UIImageView *iv = [[UIImageView alloc] initWithImage:image];

    [self.photoScrollView addSubview:iv];

    [iv sizeToFit];
    iv.contentMode = UIViewContentModeScaleAspectFit;

}

Thanks in advance!

Jay
  • 998
  • 2
  • 15
  • 24

1 Answers1

0
    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.photoScrollView.contentSize = CGSizeMake(320, 568);
        self.photoScrollView.clipsToBounds = YES;

        self.photoScrollView.maximumZoomScale = 3.0;
        self.photoScrollView.minimumZoomScale = 1.0;

        UIImage *image = [UIImage imageNamed:@"image"];
        UIImageView *iv = [[UIImageView alloc] initWithImage:image];

        [self.photoScrollView addSubview:iv];


        iv.contentMode = UIViewContentModeScaleAspectFit;
        iv.frame=CGRectMake(0, 0, 320, 568); // update this line
// Declare UIImageView* imgView property then assign self.imgView=iv;

    }

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return self.imgView;
}
Pandey_Laxman
  • 3,889
  • 2
  • 21
  • 39
  • 1
    Thanks. It works. Can you tell me more about how to zoom in and out? I know I should add the delegate method: viewForZoomingInScrollView. I want to know whether should I reset the contentsize of the scrollview in code? Or the contentsize will be reset automatically when pinch happens? – Jay Apr 26 '14 at 13:45
  • Check my updated answer, I have added zooming scale and UIScrollView Delegate method – Pandey_Laxman Apr 26 '14 at 13:52
  • @Reggie glad to help you , can you up vote my answer? – Pandey_Laxman Apr 26 '14 at 14:08
  • 1
    Sorry, I would like to vote your answer.But because I am new to stackoverflow and I just have 3 reputation, while voting up requires at least 15 reputation. So I have no right to vote and the only thing I can do is to accept your answer. – Jay Apr 26 '14 at 14:13