代码之家  ›  专栏  ›  技术社区  ›  Alvin John Tandoc

获取CGAffineTransform xcode中的缩放值和旋转值

  •  1
  • Alvin John Tandoc  · 技术社区  · 10 年前

    我正在开发一个应用程序,用户可以在上面捏和旋转图像。捏紧并旋转图像后。必须打印图像。我想得到我的图像的缩放因子和旋转因子,这样我就可以绘制新图像,然后打印新图像。但我的问题是我无法获得图像的缩放和旋转因子。有人能教我如何使用CGAffineTransform获得缩放因子和旋转因子吗。或者有人可以教我如何在缩放和旋转后保存图像。非常感谢你们。这是我的密码。

    - (void)rotationImage:(UIRotationGestureRecognizer*)gesture {
        [self.view bringSubviewToFront:gesture.view];
    
        if ([gesture state] == UIGestureRecognizerStateEnded) {
            lastRotation = 0;
            return;
        }
        CGAffineTransform currentTransform = img_tempImage.transform;
        CGFloat rotation = 0.0 - (lastRotation - gesture.rotation);
        CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, rotation);
        img_tempImage.transform = newTransform;
        myTransform = img_tempImage.transform;
        lastRotation = gesture.rotation;
        isTransform = YES;
    
    }
    
    - (void)pinchImage:(UIPinchGestureRecognizer*)pinchGestureRecognizer {
        [self.view bringSubviewToFront:pinchGestureRecognizer.view];
        pinchGestureRecognizer.view.transform = CGAffineTransformScale(pinchGestureRecognizer.view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
        pinchGestureRecognizer.scale = 1;
        myTransform = img_tempImage.transform;
        isTransform = YES;
    }
    
    - (void)panpan:(UIPanGestureRecognizer *)sender {
        [self.view bringSubviewToFront:img_tempImage];
        CGPoint translation = [sender translationInView:self.view];
        CGPoint imageViewPosition = img_tempImage.center;
        imageViewPosition.x += translation.x;
        imageViewPosition.y += translation.y;
        myPoint = imageViewPosition;
        img_tempImage.center = imageViewPosition;
        [sender setTranslation:CGPointZero inView:self.view];
    }
    
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
        return  YES;
    }
    
    1 回复  |  直到 10 年前
        1
  •  3
  •   Alvin John Tandoc    10 年前

    我找到了关于如何获得刻度和旋转值的问题的答案。

     CGFloat angle = [(NSNumber *)[img_tempImage valueForKeyPath:@"layer.transform.rotation.z"] floatValue];
        NSLog(@"by angle: %.2f",angle);
        CGFloat xScale = img_tempImage.transform.a;
        CGFloat yScale = img_tempImage.transform.d;
        NSLog(@"xScale :%.2f",xScale);
        NSLog(@"yScale :%.2f",yScale);