确定裁剪矩形是否完全包含在旋转的UIView中 [英] Determine if crop rect is entirely contained within rotated UIView

查看:94
本文介绍了确定裁剪矩形是否完全包含在旋转的UIView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前提:我正在构建一个裁剪工具,可以处理双指任意旋转图像以及任意裁剪。

Premise: I'm building a cropping tool that handles two-finger arbitrary rotation of an image as well as arbitrary cropping.

有时图像最终会旋转插入空白空间以填充旋转图像和裁剪矩形之间的间隙的方法(参见下面的示例)。

Sometimes the image ends up rotated in a way that empty space is inserted to fill a gap between the rotated image and the crop rect (see the examples below).

我需要确保图像视图,旋转时,完全适合裁剪矩形。如果没有,我需要重新转换图像(缩放),使其适合裁剪边界。

I need to ensure that the image view, when rotated, fits entirely into the cropping rectangle. If it doesn't, I then need to re-transform the image (zoom it) so that it fits into the crop bounds.

使用这个答案,我已经实现了检查旋转的UIImageView是否相交的功能使用裁剪CGRect,但不幸的是,这并没有告诉我旋转的imageview中裁剪矩是否完全。希望我能对这个答案做一些简单的修改吗?

Using this answer, I've implemented the ability to check whether a rotated UIImageView intersects with the cropping CGRect, but unfortunately that doesn't tell me if the crop rect is entirely contained in the rotated imageview. Hoping that I can make some easy modifications to this answer?

OK的可视化示例:

并且不行,我是需要检测和处理:

and not OK, that which I need to detect and deal with:

更新:不工作方式

- (BOOL)rotatedView:(UIView*)rotatedView containsViewCompletely:(UIView*)containedView {

    CGRect rotatedBounds = rotatedView.bounds;
    CGPoint polyContainedView[4];

    polyContainedView[0] = [containedView convertPoint:rotatedBounds.origin toView:rotatedView];
    polyContainedView[1] = [containedView convertPoint:CGPointMake(rotatedBounds.origin.x + rotatedBounds.size.width, rotatedBounds.origin.y) toView:rotatedView];
    polyContainedView[2] = [containedView convertPoint:CGPointMake(rotatedBounds.origin.x + rotatedBounds.size.width, rotatedBounds.origin.y + rotatedBounds.size.height) toView:rotatedView];
    polyContainedView[3] = [containedView convertPoint:CGPointMake(rotatedBounds.origin.x, rotatedBounds.origin.y + rotatedBounds.size.height) toView:rotatedView];

    if (CGRectContainsPoint(rotatedView.bounds, polyContainedView[0]) &&
        CGRectContainsPoint(rotatedView.bounds, polyContainedView[1]) &&
        CGRectContainsPoint(rotatedView.bounds, polyContainedView[2]) &&
        CGRectContainsPoint(rotatedView.bounds, polyContainedView[3]))
        return YES;
    else
        return NO;
}


推荐答案

这应该比检查更容易用于交集(如在引用的线程中)。

That should be easier than checking for intersection (as in the referenced thread).

(旋转的)图像视图是四边形。因此,检查
就足以使裁剪矩形的所有4个角点都在旋转的图像视图中。

The (rotated) image view is a convex quadrilateral. Therefore it suffices to check that all 4 corner points of the crop rectangle are within the rotated image view.


  • 使用 [cropView convertPoint:指向view:imageView] 将裁剪矩形的角点转换为
    (旋转)图像的坐标系查看。

  • 使用 CGRectContainsPoint()检查4个转换的角点是否在范围内图像视图的矩形。

  • Use [cropView convertPoint:point toView:imageView] to convert the corner points of the crop rectangle to the coordinate system of the (rotated) image view.
  • Use CGRectContainsPoint() to check that the 4 converted corner points are within the bounds rectangle of the image view.

示例代码:

- (BOOL)rotatedView:(UIView *)rotatedView containsCompletely:(UIView *)cropView {

    CGPoint cropRotated[4];
    CGRect rotatedBounds = rotatedView.bounds;
    CGRect cropBounds = cropView.bounds;

    // Convert corner points of cropView to the coordinate system of rotatedView:
    cropRotated[0] = [cropView convertPoint:cropBounds.origin toView:rotatedView];
    cropRotated[1] = [cropView convertPoint:CGPointMake(cropBounds.origin.x + cropBounds.size.width, cropBounds.origin.y) toView:rotatedView];
    cropRotated[2] = [cropView convertPoint:CGPointMake(cropBounds.origin.x + cropBounds.size.width, cropBounds.origin.y + cropBounds.size.height) toView:rotatedView];
    cropRotated[3] = [cropView convertPoint:CGPointMake(cropBounds.origin.x, cropBounds.origin.y + cropBounds.size.height) toView:rotatedView];

    // Check if all converted points are within the bounds of rotatedView:
    return (CGRectContainsPoint(rotatedBounds, cropRotated[0]) &&
            CGRectContainsPoint(rotatedBounds, cropRotated[1]) &&
            CGRectContainsPoint(rotatedBounds, cropRotated[2]) &&
            CGRectContainsPoint(rotatedBounds, cropRotated[3]));
}

这篇关于确定裁剪矩形是否完全包含在旋转的UIView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆