【问题标题】:Image rotation Issues in IphoneIphone 中的图像旋转问题
【发布时间】:2012-03-16 04:44:58
【问题描述】:

我想以 90 度角旋转图像(左右旋转)。我使用以下代码以 360 度角向左旋转。如何以 90 度角旋转图像。

-(IBAction)btnLeftRotateClicked:(id)sender
{


    CATransform3D rotationTransform = CATransform3DMakeRotation(1.0f * M_PI, 0, 0, 1.0);

    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];

    rotationAnimation.toValue = [NSValue valueWithCATransform3D:rotationTransform];
    rotationAnimation.duration = 0.25f;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1; 

    [imageAdjustView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}

谢谢。

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    如果你只想在 2D 中旋转图像,为什么不使用 affineTransform

    CGAffineTransformMakeRotation(-M_PI * 0.5);
    

    【讨论】:

    【解决方案2】:

    360 度的旋转是 no 旋转。猜你的意思是 180 = PI。因此,90 度 = .5*PI。

    【讨论】:

    • 我尝试 .5*M_PI 但同样的问题。
    【解决方案3】:

    如果您想使用一些简单的类来旋转 UIImage 并对其进行缩放,请尝试查看:http://bit.ly/w3r5t6 这是一个名为 WBImage 的 UIImage 类别

    【讨论】:

      【解决方案4】:

      @M.S.B:

      我认为这个链接会对你有所帮助。

      How to Rotate a UIImage 90 degrees?

      How to programmatically rotate image by 90 Degrees in iPhone?

      您可以在上面的链接中通过 fbrereto 参考这个答案

      类似的东西呢:

      static inline double radians (double degrees) {return degrees * M_PI/180;}
      UIImage* rotate(UIImage* src, UIImageOrientation orientation)
      {
          UIGraphicsBeginImageContext(src.size);
      
          CGContextRef context = UIGraphicsGetCurrentContext();
      
          if (orientation == UIImageOrientationRight) {
              CGContextRotateCTM (context, radians(90));
          } else if (orientation == UIImageOrientationLeft) {
              CGContextRotateCTM (context, radians(-90));
          } else if (orientation == UIImageOrientationDown) {
              // NOTHING
          } else if (orientation == UIImageOrientationUp) {
              CGContextRotateCTM (context, radians(90));
          }
      
          [src drawAtPoint:CGPointMake(0, 0)];
      
          return UIGraphicsGetImageFromCurrentImageContext();
      }
      

      编辑:您可以在链接How to programmatically rotate image by 90 Degrees in iPhone? 下参考Sabobin 给出的答案。我已经发布了相同的代码,以便您可以从这里参考:

      UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"my_image.png"]];
      
      //set point of rotation
      myImageView.center = CGPointMake(100.0, 100.0);
      
      //rotate rect
      myImageView.transform = CGAffineTransformMakeRotation(3.14159265/2);
      

      希望这对您有所帮助。

      【讨论】:

      • @M.S.B:我刚刚编辑了我的答案,让您更简单。检查我的答案的 EDIT 部分。我认为你可以直接申请
      • 您编辑的代码我使用,但它只旋转一次我希望每次点击它都会旋转。顺便说一句,如果您有翻转镜像水平代码,您可以与我分享吗?
      • @M.S.B:你能在你做出改变后发布你的代码吗?
      • -(IBAction)btnRightRotateClicked:(id)sender { rotation=rotation-90; CGAffineTransform 变换 = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-rotation)); imageAdjustView.transform = 变换; [UIView 提交动画];我现在用它来进行右旋转它的工作正常。
      • UIImage* rotate(UIImage* src, UIImageOrientationorientation) 在你的函数中我不明白它的类方法或实例方法。你能正确的示例代码如何调用这个函数吗?
      猜你喜欢
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多