寻找具有透明背景的旋转图像的边界框

时间:2013-01-27 15:08:04

标签: iphone ios cocoa-touch uiimage core-graphics

我有UIImage,背景透明。旋转此图像时,我想找到图形周围的边界框(即非透明部分,如果在UIImageView中旋转它,它将找到整个UIImage周围的边界框包括透明部分)。

Apple库是否可以为我这样做?如果没有,有谁知道如何做到这一点?

1 个答案:

答案 0 :(得分:1)

如果我正确理解了你的问题,你可以检索uiimageview的框架(不是边界),然后获取各个cgpoints并显式转换这些点以获得转换的矩形。因为在Apple的文档中它说:您可以通过调用函数CGRectApplyAffineTransform来操作CGRect结构。此函数返回包含传递给它的矩形的变换角点的最小矩形。 1点变换点应该避免这种自动校正行为。

CGRect originalFrame = UIImageView.frame;
CGPoint p1 = originalFrame.origin;
CGPoint p2 = p1; p2.x += originalFrame.width;
CGPoint p3 = p1; p3.y += originalFrame.height;

//Use the same transformation that you applied to uiimageview here
CGPoint transformedP1 = CGPointApplyAffineTransform(p1, transform);
CGPoint transformedP2 = CGPointApplyAffineTransform(p2, transform);
CGPoint transformedP3 = CGPointApplyAffineTransform(p3, transform);

现在你应该可以从这3个点定义一个新的矩形(第4个是可选的,因为宽度和高度可以从3个点计算。需要注意的一点是你不能将这个新的矩形存储在cgrect中因为cgrect由原点和大小定义,因此它的边缘始终与x和y轴平行.Apple的cgrect定义不允许存储旋转的矩形。

相关问题