gpt4 book ai didi

ios 旋转图像和缩放,所以它适合像 instagram 中的原始框架

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:02 25 4
gpt4 key购买 nike

在 Instagram 中,当您旋转图像时它会缩放,因此您看不到那些有角度的角。太完美了。

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

我想知道是否有任何开箱即用的解决方案,一些包含此功能的库。

我知道如何旋转或缩放图像。只是不知道根据纵横比和角度旋转后应该缩放多少。

最佳答案

我不知道有任何库,但以下方法应该可以提供您所需要的。假设您有一个大小为 contentSize 的 View ,您想要旋转 angle 弧度并缩放以完全填充(没有“空”角)大小为 容器大小<​​。下面的第一个方法将返回(作为 double)您应该应用的比例因子。

为了完整起见,如果您希望通过 angle 弧度旋转大小为 contentSize 的 View 并缩放它以确保是否完全适合,则下面的第二种方法将提供缩放因子在 containerSize 范围内(因此它的角都没有超出边缘)。

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

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

关于ios 旋转图像和缩放,所以它适合像 instagram 中的原始框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26307116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com