1

UIImageView我用这种方法创建了一个底部一层和一层覆盖的双层:

UIImage *bottomImage = self.imageView.image;
UIImage *image       = self.urlFoto.image;

CGSize newSize = CGSizeMake(640, 640);
UIGraphicsBeginImageContext( newSize );

[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

在此之后,我添加了一种使用捏合手势调整图像大小的方法:

- (IBAction)scaleImage:(UIPinchGestureRecognizer *)recognizer {

    recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
    recognizer.scale = 1;
}

但是当我将照片保存在照片库中时,我无法获得叠加层的当前大小,并且在叠加层(图像)上看到相同的大小 640,640,我认为错过的代码在这里:

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];

在任何知道正确的方法来获得捏后CGRectMake(0,0,newSize.width,newSize.height)的当前大小?UIImageView

4

2 回答 2

2

绘制时,必须牢记比例变换。UIImageView因此,您必须根据通过手势调整的最终比例来绘制叠加图像。

您可以像这样获得最终的视图比例:

CGFloat scale = view.transform.a;

这封信a在这里很重要。它是宽度变换的值。因此,假设您按比例缩放图像(宽度和高度的比例相同),您可以使用它来获得通用比例


关于规模的更多细节:

CGAffineTransform结构定义为

struct CGAffineTransform {
  CGFloat a, b, c, d;
  CGFloat tx, ty;
};

CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)

根据文档执行以下操作

返回一个按 `(sx, sy)' 缩放的变换:

t' = [ sx 0 0 sy 0 0 ]

为了更好地理解,请参阅此示例代码:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];

view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);
NSLog(@"Transform 1: %f", view.transform.a);

view.transform = CGAffineTransformScale(view.transform, 0.5, 0.5);
NSLog(@"Transform 2: %f", view.transform.a);

将以下内容打印到控制台:

Transform 1: 0.500000
Transform 2: 0.250000
  1. 第一次变换使 0.5 缩放到默认比例 1.0
  2. 第二次变换使 0.5 缩放到已经缩放的 0.5 -> 将当前 0.5 与新的 0.5 缩放相乘
  3. 等等
于 2013-09-24T13:58:44.023 回答
0

Step 1: 在 .h 文件中声明

CGFloat lastScale;

Step 2: 在 .m 文件中替换你的手势方法

- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        // Reset the last scale, necessary if there are multiple objects with different scales
        lastScale = [gestureRecognizer scale];
    }

    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
        [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

        CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

        // Calculate the New Scale of UIImageView
        CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]);

        // Store Your Imageview's transform
        CGAffineTransform transorm = simageView.transform;
        // Convert your Imageview to Identity (original Size)
        [imageView setTransform:CGAffineTransformIdentity];
        // Save Rect Of UIImageView
        CGRect actualRect = self.view.frame;
        // Apply Transform to Imageview to make it scaled
        [imageView setTransform:transorm];

        // Now calculate new frame size of your ImageView
        NSLog(@"width %f height %f",(imageView.frame.size.width*newScale), (imageView.frame.size.height*newScale));
    }
}
于 2013-09-24T15:00:18.987 回答