Search code examples
ios5uiimageviewquartz-2dimage-rotationzooming

UIImageView Rotation and zooming in iPhone Sdk


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


Solution

  • 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);
     }