objective-c - 应用 CGAffineTransform 后从 imageview 获取新的图像尺寸

标签 objective-c ios uiimageview cgaffinetransform

我正在使用 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);

谢谢大家

最佳答案

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

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

关于objective-c - 应用 CGAffineTransform 后从 imageview 获取新的图像尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10297842/

相关文章:

iphone - AudioServicesCreateSystemSoundID 是做什么的?

objective-c - NSDataDetector 检测 "phone number"文本

iOS:将 NSArray 放在 NSString 中

ios - 解析具有不同本地的 JSON 字符串(无英文)

ios - UIViewContentModeScaleAspectFill 不保持图像的纵横比

ios - 如何使用 MFMailComposeViewController 发送图像

iphone - 如何通过代码设置UIImageView?

ruby-on-rails - SHA1 哈希值在我的 Rails 和 Cocoa 应用程序之间不匹配

ios - Swift 分离 UITableViewDataSource

ios - 具有动态约束的 IBDesignable View : Misplaced Children when Rendering