0

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

VSN
  • 2,361
  • 22
  • 30

3 Answers3

1

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);
}
John Smith
  • 2,012
  • 1
  • 21
  • 33
  • And play a little with that "0.3" value to gain the desired zooming factor. – John Smith Jul 19 '12 at 11:09
  • Teodor, it should be work zooming while rotating the image also... Here is the scenario... if i click zoomin and zoomout methods its working fine... after some time when i call rotation function, the image position is coming to normal instead of rotation position. – VSN Jul 19 '12 at 11:34
  • Perhaps you reset somewhere the "degrees" value. Make sure it is not reset, and also, make sure to keep it always between zero and 360 ( like if degree is more than 360 then degree = zero , and viceversa if it is less than zero, then degree = 360) – John Smith Jul 19 '12 at 12:45
1

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;

Mazyod
  • 22,319
  • 10
  • 92
  • 157
1

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);
 }
VSN
  • 2,361
  • 22
  • 30