获取UIImageView的当前大小

时间:2013-09-24 13:37:09

标签: objective-c uiimageview resize pinchzoom

我在底部创建了一个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的当前大小?

2 个答案:

答案 0 :(得分: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比例相乘

答案 1 :(得分: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));
    }
}