【问题标题】:Get the new size of image from imageview after applying CGAffineTransform应用 CGAffineTransform 后从 imageview 获取图像的新大小
【发布时间】:2012-04-24 12:30:28
【问题描述】:

我正在使用 UIGestureRecognizers 使用 uiimageview 旋转和调整图像大小。现在的问题是我需要从 UIImageView 变换(即 CGAffineTransform)属性中获取图像的新大小。我在 StackOverFlow 上找到了这段代码,但它没有返回正确的值。由于 imageview 不仅调整大小而且旋转。所以我的问题是如何从旋转和调整大小的 UIImageView 的变换属性中获取新的大小和位置?

CGRect newRect = CGRectApplyAffineTransform(self.imageView.bounds, self.imageView.transform);

有人可以帮忙吗?

谢谢

回答 好的,我找到了解决方法。造成尺寸计算问题的主要原因是旋转。那么这里做了什么检查。

 CGFloat angle = atan2(self.imageView.transform.b, self.imageView.transform.a);
 CGAffineTransform tempTransform = CGAffineTransformRotate(self.imageView.transform, -angle);  
 CGRect newRect = CGRectApplyAffineTransform(CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height), tempTransform);

谢谢大家

【问题讨论】:

    标签: objective-c ios uiimageview cgaffinetransform


    【解决方案1】:

    您可以使用临时点来模拟尺寸尺寸的变换,如下所示:

    CG_INLINE CGSize FAE_CGSizeCalcDimentionsAfterApplyTransformation(CGSize size, CGAffineTransform transform)
    {
        CGPoint widthPoint = CGPointMake(size.width, 0.0);
        CGPoint heightPoint = CGPointMake(0.0, size.height);
    
        CGPoint centerAfterTransfrom = CGPointApplyAffineTransform(CGPointZero, transform);
        CGPoint widthAfterTransform = CGPointApplyAffineTransform(widthPoint, transform);
        CGPoint heightAfterTransform = CGPointApplyAffineTransform(heightPoint, transform);
    
        CGSize sizeAfterTransformation = CGSizeMake(TB_CGPointCalcDistance(centerAfterTransfrom, widthAfterTransform), TB_CGPointCalcDistance(centerAfterTransfrom, heightAfterTransform));
    
        return sizeAfterTransformation;
    }
    

    【讨论】:

    • 什么是 TB_CGPointCalcDistance?
    • TB_CGPointCalcDistance 是用于计算点之间距离的自定义函数:sqrt(dxdx + dydy)
    【解决方案2】:

    来自苹果doc

    因为仿射变换一般不保留矩形,所以 函数 CGRectApplyAffineTransform 返回最小的矩形 包含 rect 参数的转换角点。

    也许你应该试试这个:CGSizeApplyAffineTransform:

    返回由一个转换产生的高度和宽度 现有的高度和宽度。

    尝试使用 imageView 的旧尺寸:

    CGSize oldImageSize = imageView.bounds.size;
    
    // Transforming code here
    
    CGAffineTransform currentTransform = imageView.transform;
    CGSize newImageSize = CGSizeApplyAffineTransform(oldImageSize, currentTransform);
    

    【讨论】:

    • 谢谢!但我已经试过了,这个函数正确返回宽度但高度值不准确。有什么想法吗?
    • 嗨,你得到这个答案了吗,请帮助我@Jonathan Naguin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多