【问题标题】:UIImageView Rotation and zooming in iPhone SdkiPhone Sdk 中的 UIImageView 旋转和缩放
【发布时间】:2012-07-19 10:51:49
【问题描述】:

我正在我的项目中使用 UIImageview 实现缩放和旋转, 旋转图像后,我在放大和缩小时遇到问题,

我的代码如下:

在.h文件中

@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; 

在.m文件中

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

请找到附件图片供您参考。

【问题讨论】:

  • 它的缩放,但图像像给定的参考图像一样拉伸。

标签: ios5 uiimageview quartz-2d image-rotation zooming


【解决方案1】:

您应该使用CGAffineTransformMakeScale 进行缩放,而不是强制使用框架。

然后在某处定义一个全局foal x = 1;

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

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

【讨论】:

  • 并使用“0.3”值稍微调整一下以获得所需的缩放系数。
  • Teodor,它应该在旋转图像的同时进行缩放......这是场景......如果我点击放大和缩小方法它工作正常......经过一段时间我调用旋转函数,图像位置恢复正常而不是旋转位置。
  • 也许您在某处重置了“度”值。确保它没有被重置,并且,确保它始终保持在 0 和 360 之间(比如如果 degree 大于 360,则 degree = zero ,反之亦然,如果它小于零,则 degree = 360)
【解决方案2】:

我建议使用与您拥有的旋转代码非常相似的方法来缩放图像:

CGAffineTransformMakeScale(CGFloat sx, CGFloat sy);

发送大于 1.0 放大,小于 1.0 缩小;

【讨论】:

    【解决方案3】:

    下面的代码非常适合我!!!

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

    【讨论】:

      猜你喜欢
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      相关资源
      最近更新 更多