Monday 29 April 2013

Zoom IN & Zoom OUT using a Image in Xcode

Description:

This code can be used to Zoom In & Zoom Out options for images.


.h File:


@interface view : UIViewController
{
    IBOutlet UIScrollView *scrollView;
 
    IBOutlet UIImageView *imageView;
}
@end


.m File:


- (void)viewDidLoad
{
    [super viewDidLoad];
 
    UIImage *image = [UIImage imageNamed:@"demo.png"];
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
    [imageView setImage:image];
 
    CGRect yFrame = CGRectMake(0.0, 0.0, 320, 432);
    scrollView = [[UIScrollView alloc] initWithFrame:yFrame];
    [scrollView setScrollEnabled:YES];
    [scrollView setBackgroundColor:[UIColor blackColor]];
    [scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
    scrollView.minimumZoomScale = 0.4;
    scrollView.maximumZoomScale = 4.0;
    [scrollView setZoomScale:scrollView.minimumZoomScale];
    scrollView.delegate = self;
    [scrollView addSubview:imageView];

    // For set a Zoom out the image
    //scrollView.zoomScale = 0.4;

    [self.view addSubview:scrollView];
    [self.view bringSubviewToFront:scrollView];  
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}


1 comment:

Shashi Tiwari said...

Thanks!! It works for me..
You have done it in very simple way :)