Ios 确保在发生旋转时,任意旋转的CGRect填充另一个CGRect

Ios 确保在发生旋转时,任意旋转的CGRect填充另一个CGRect,ios,objective-c,geometry,image-rotation,Ios,Objective C,Geometry,Image Rotation,更新:下面的部分工作实施 我以前和现在都问了几个问题 第一种方法非常适用于确定“image”rect是否充分包含在“crop”rect中。 第二种方法有点有效,但在我的实现中出现了一些问题,它实际上不起作用 我现在对这个问题的看法有点不同,我想改变这种行为: 当用户开始旋转图像时,我将运行检查方法(如下)来确定它是否需要修复 如果它确实需要修复,而不是等待用户完成旋转,我想同时调整图像大小以适应边界。是否有更简单(或更可靠)的方法来实现此行为 我将阻止旋转在任何方向大于35º,这样我们就不必担心

更新:下面的部分工作实施

我以前和现在都问了几个问题

第一种方法非常适用于确定“image”rect是否充分包含在“crop”rect中。 第二种方法有点有效,但在我的实现中出现了一些问题,它实际上不起作用

我现在对这个问题的看法有点不同,我想改变这种行为:

  • 当用户开始旋转图像时,我将运行检查方法(如下)来确定它是否需要修复
  • 如果它确实需要修复,而不是等待用户完成旋转,我想同时调整图像大小以适应边界。是否有更简单(或更可靠)的方法来实现此行为
  • 我将阻止旋转在任何方向大于35º,这样我们就不必担心严重的放大
  • 假设/限制:

  • 我正在使用自动布局
  • 旋转点将是裁剪矩形的中心,它可能是也可能不是图像矩形的中心
  • 这演示了如何使用方形裁剪,但用户可以将其调整为任何大小,所以我想当它不是方形时,它会更咬我的屁股
  • 代码:

    即使是在这个问题上采取不同的立场,我也会达到目的。但正如你在.gif文件中看到的,最终计算结果会出错,因为我不应该用旋转来计算新的大小。如何使用正确的几何体实现此功能,以确保图像始终正确调整大小?为了节省时间,我将其放在一个Xcode项目中,这样您就不必忙于构建自己的:

    -(iAction)手势识别:(UIRotationGestureRecognitizer*)手势{
    CGFloat maxRotation=40;
    CGFloat旋转=手势旋转;
    CGFloat currentRotation=atan2f(_imageView.transform.b,_imageView.transform.a);;
    NSLog(@“%0.4f”,弧度到度(旋转));
    if((当前旋转>度到弧度(最大旋转)和旋转>0)|(当前旋转<度到弧度(-最大旋转)和旋转<0)){
    回来
    }
    CGAffineTransform rotationTransform=CGAffineTransformRotate(self.imageView.transform,rotation);
    手势旋转=0.0f;
    如果(手势.state==UIGestureRecognitizerStateChanged){
    CGFloat scale=sqrt(_imageView.transform.a*_imageView.transform.a+_imageView.transform.c*_imageView.transform.c);
    if((当前旋转>0和旋转>0)| |(当前旋转<0和旋转<0))
    比例=1+晶圆厂(旋转);
    else if(currentRotation==0)
    比例=1;
    其他的
    比例=1-晶圆厂(轮换);
    CGAffineTransform sizeTransform=CGAffineTransformMakeScale(比例,比例);
    CGPoint center=_imageView.center;
    _imageView.transform=CGAffineTransformConcat(旋转变换,sizeTransform);
    _imageView.center=中心;
    }
    }
    
    Gif:

    - (BOOL)rotatedView:(UIView*)rotatedView containsViewCompletely:(UIView*)cropView {
        // If this method returns YES, good! if NO, bad!
    
        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]));
    }
    
    - (IBAction)gestureRecognized:(UIRotationGestureRecognizer *)gesture {
        CGFloat maxRotation = 40;
        CGFloat rotation = gesture.rotation;
        CGFloat currentRotation = atan2f(_imageView.transform.b, _imageView.transform.a);;
    
        NSLog(@"%0.4f", RADIANS_TO_DEGREES(rotation));
        if ((currentRotation > DEGREES_TO_RADIANS(maxRotation) && rotation > 0) || (currentRotation < DEGREES_TO_RADIANS(-maxRotation) && rotation < 0)) {
            return;
        }
    
        CGAffineTransform rotationTransform = CGAffineTransformRotate(self.imageView.transform, rotation);
        gesture.rotation = 0.0f;
    
        if (gesture.state == UIGestureRecognizerStateChanged) {
            CGFloat scale = sqrt(_imageView.transform.a * _imageView.transform.a + _imageView.transform.c * _imageView.transform.c);
    
            if ((currentRotation > 0 && rotation > 0) || (currentRotation < 0 && rotation < 0))
                scale = 1 + fabs(rotation);
            else if (currentRotation == 0)
                scale = 1;
            else
                scale = 1 - fabs(rotation);
    
            CGAffineTransform sizeTransform = CGAffineTransformMakeScale(scale, scale);
            CGPoint center = _imageView.center;
            _imageView.transform = CGAffineTransformConcat(rotationTransform, sizeTransform);
            _imageView.center = center;
        }
    }