Scale UIImage in IOS

by / Friday, 27 September 2013 / Published in Ipad, Iphone

A UIImage object is a high-level way to display image data. You can create images from files, from Quartz image objects, or from raw image data you receive. You can also change scale of UIImage. i.e. you can zoom in or zoom out UIImage with pinch gesture First of all in your .h file write the following code-

@interface ViewController : UIViewController<UIGestureRecognizerDelegate>

@property (retain, nonatomic) IBOutlet UIImageView *imgView;

In .m file inside viewDidLoad method write the following code-

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];

[self.view addGestureRecognizer:pinchRecognizer];

pinchRecognizer.delegate = self;

Here we will change the scale of the UIImage simultaneously with the pinch gesture. To make the pinch gesture work write the following two methods-

- (void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer

{

CGFloat scale = pinchRecognizer.scale;

UIImageView * newImageVw=[[UIImageView alloc] initWithFrame:self.imgView.frame];

newImageVw.transform = CGAffineTransformScale(newImageVw.transform, scale, scale);

float x1=newImageVw.frame.origin.x+newImageVw.frame.size.width;

float y1=newImageVw.frame.origin.y+newImageVw.frame.size.height;

if(newImageVw.frame.origin.x>=0 && newImageVw.frame.origin.y>=0 && x1<=self.view.frame.size.width && y1<=self.view.frame.size.height){

self.imgView.transform = CGAffineTransformScale(self.imgView.transform, scale, scale);

}

[newImageVw release];

pinchRecognizer.scale = 1.0;

}

 

#pragma mark - UIGestureRecognizerDelegate

 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

{

return YES;

}

for reference click here

TOP