2014-09-13 55 views
1

我正在開發一個應用程序,用戶可以在其中捏和旋轉圖像。捏和旋轉圖像後。圖像必須打印。我想獲得圖像的縮放因子和旋轉因子,以便我可以繪製新圖像然後打印新圖像。但我的問題是我不能得到我的形象的縮放和旋轉因素。任何人都可以教我如何使用CGAffineTransform來獲得比例因子和旋轉因子。或者任何人都可以教我如何在縮放和旋轉後保存圖像。十分感謝大家。這是我的代碼。獲取比例值並在CGAffineTransform中旋轉值xcode

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

回答

3

我在如何獲得比例和旋轉值的問題上找到了答案。

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