Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Zoom an image using UIScrollView

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 316
    Comment on it

    The UIScrollView is used to view a content whose size is larger than the screen of the device. It can also be used to zoom in and zoom out an image, or to view an image that is significantly larger than the screen of the device. To make the scroll view zoom in and zoom out an image, follow the steps as given below.

    - (void)viewDidLoad
    {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
        scroll.backgroundColor = [UIColor blackColor];
        scroll.delegate = self;
        image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.JPG"]];
        scroll.contentSize = image.frame.size;
        [scroll addSubview:image];
    
        scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
        scroll.maximumZoomScale = 2.0;
        [scroll setZoomScale:scroll.minimumZoomScale];
    
        self.view = scroll;
        [scroll release];
    
    }
    
    - (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView
    {
        CGSize boundsSize = scroll.bounds.size;
        CGRect frameToCenter = rView.frame;
    
        // center horizontally
        if (frameToCenter.size.width < boundsSize.width) 
    {
            frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
        }
        else {
            frameToCenter.origin.x = 0;
        }
    
        // center vertically
        if (frameToCenter.size.height < boundsSize.height) 
        {
            frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
        }
        else 
        {
            frameToCenter.origin.y = 0;
        }
    
        return frameToCenter;
    }
    
    #pragma mark UIScrollViewDelegate
    
    - (void)scrollViewDidZoom:(UIScrollView *)scrollView 
    {
       image.frame = [self centeredFrameForScrollView:scrollView andUIView:image];;
    }
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
    {
        return image;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: