ios旋转图像并缩放,所以它像instagram中一样适合原始帧



在instagram中,当你旋转图像时,它会缩放,所以你看不到那些有角度的角落。这太完美了。

此处讨论此主题:https://math.stackexchange.com/questions/61005/resizing-a-rectangle-to-always-fit-into-its-unrotated-space

我想知道是否有现成的解决方案,一些库会包含这个函数。

我知道如何旋转或缩放图像。我只是不知道根据长宽比和角度旋转后的变焦应该是多少。

我不知道有什么库,但以下方法应该能提供您所需要的。假设您有一个尺寸为contentSize的视图,您希望通过angle弧度和缩放来完全填充(没有"空"角)尺寸为containerSize的视图。下面的第一个方法将返回(作为double)您应该应用的比例因子。

为了完整起见,如果您希望将尺寸为contentSizeangle弧度的视图旋转并缩放以确保其完全适合containerSize(因此其角都不会延伸到边之外),则下面的第二种方法将提供缩放因子。

两种方法都假定这两种视图具有相同的中心。

-(double)scaleToFill:(CGSize)containerSize withSize:(CGSize)contentSize atAngle:(double)angle {
    double theta = fabs(angle - 2*M_PI*truncf(angle/M_PI/2) - M_PI);
    if (theta > M_PI_2) {
        theta = fabs(M_PI - theta);
    }
    double h = contentSize.height;
    double H = containerSize.height;
    double w = contentSize.width;
    double W = containerSize.width;
    double scale1 = (H*cos(theta) + W*sin(theta))/h;
    double scale2 = (H*sin(theta) + W*cos(theta))/w;
    double scaleFactor = MAX(scale1, scale2);
    return scaleFactor;
}
-(double)scaleToFit:(CGSize)contentSize atAngle:(double)angle withinSize:(CGSize)containerSize {
    double theta = fabs(angle - 2*M_PI*truncf(angle/M_PI/2) - M_PI);
    if (theta > M_PI_2) {
        theta = fabs(M_PI - theta);
    }
    double h = contentSize.height;
    double H = containerSize.height;
    double w = contentSize.width;
    double W = containerSize.width;
    double scale1 = H/(h*cos(theta) + w*sin(theta));
    double scale2 = W/(h*sin(theta) + w*cos(theta));
    double scaleFactor = MIN(scale1, scale2);
    return scaleFactor;
}

您可以将这些与以下内容一起使用:

double scaleFactor = [self scaleToFill:self.containerView.bounds.size withSize:self.rotatedView.bounds.size atAngle:self.currentAngle];
CGAffineTransform rotation = CGAffineTransformMakeRotation(self.currentAngle);
CGAffineTransform scaledRotation = CGAffineTransformScale(rotation, scaleFactor, scaleFactor);
self.rotatedView.transform = scaledRotation;

最新更新