0
votes

i'm implementing zooming and rotation using UIImageview in my project, i'm facing problem in zoom in and zoom out after rotating the image,

Here is my code follows:

in .h file

@interface ViewController : UIViewController{
 float degrees;
 float height;
 float width;

float moveLeft;
float moveRight;
}

@property(nonatomic,retain)UIImageView *imageView;

-(IBAction)rotationLeft:(id)sender;
-(IBAction)rotationRight:(id)sender;
-(IBAction)zoomIn:(id)sender;
-(IBAction)zoomOut:(id)sender;

-(IBAction)moveLeft:(id)sender;
-(IBAction)moveRight:(id)sender; 

in .m file

- (void)viewDidLoad
{
[super viewDidLoad];



height=50;
width=50;
degrees=20;
moveLeft=20;
moveRight=20;
imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.png"]];
imageView.frame=CGRectMake(100, 100,width, height);
[self.view addSubview:imageView];



// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)rotationLeft:(id)sender{
//the value in degrees
imageView.transform = CGAffineTransformMakeRotation(degrees*M_PI/180);
degrees=degrees+25;
}

-(IBAction)rotationRight:(id)sender{
//the value in degrees
degrees=degrees-25;
imageView.transform = CGAffineTransformMakeRotation(degrees*M_PI/180);

}
-(IBAction)zoomIn:(id)sender{
height=height-15;
width=width-15;
imageView.frame=CGRectMake(100, 100,width, height);
}
-(IBAction)zoomOut:(id)sender{
height=height+15;
width=width+15;
imageView.frame=CGRectMake(100, 100,width, height);
}

Please find the attached image for your reference.enter image description here

3
its zooming but image is stretching like given reference image. - VSN

3 Answers

1
votes

you should use CGAffineTransformMakeScale for zooming purposes, instead of forcing the frame.

define somewhere a global foal x = 1; then:

-(IBAction)zoomIn:(id)sender{
   x += 0.3;
   imageView.transform = CGAffineTransformMakeScale(x, x);
}

-(IBAction)zoomOut:(id)sender{
   x -= 0.3;
   imageView.transform = CGAffineTransformMakeScale(x, x);
}
1
votes

I would recommend scaling the image using a very similar method to the rotation code that you have:

CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);

Just send it more than 1.0 to scale up and less than 1.0 to scale down;

1
votes

Below code worked for me perfect!!!

-(IBAction)rotationLeft:(id)sender{
//the value in degrees
    degrees=degrees+25;
    CGAffineTransform t;
    t=CGAffineTransformMakeScale(x, x);
    // imageView.transform = CGAffineTransformMakeRotation(degrees*M_PI/180,x,x);
    imageView.transform=CGAffineTransformRotate(t, degrees*M_PI/180);
}

-(IBAction)rotationRight:(id)sender{
    degrees=degrees-25;
    CGAffineTransform t;
    t=CGAffineTransformMakeScale(x, x);
    imageView.transform=CGAffineTransformRotate(t, degrees*M_PI/180);

 }
 -(IBAction)zoomIn:(id)sender{
    x += 0.3;
    CGAffineTransform t;
    t=CGAffineTransformMakeRotation(degrees*M_PI/180);
    imageView.transform=CGAffineTransformScale(t, x, x);
 }

 -(IBAction)zoomOut:(id)sender{
     x -= 0.3;
    CGAffineTransform t;
    t=CGAffineTransformMakeRotation(degrees*M_PI/180);
    imageView.transform=CGAffineTransformScale(t, x, x);
 }