gpt4 book ai didi

uiimageview - 从旋转的 UIImageView 创建 UIImage

转载 作者:行者123 更新时间:2023-12-03 20:21:57 26 4
gpt4 key购买 nike

我有一个带有图像的 UIImageView。我在显示之前旋转了图像,方法是将 UIImageView 的变换属性设置为 CGAffineTransformMakeRotation(angle),其中角度是以弧度为单位的角度。

我希望能够创建另一个 UIImage 对应于我可以在我的 View 中看到的旋转版本。

我快到了,通过旋转图像上下文我得到一个旋转的图像:

- (UIImage *) rotatedImageFromImageView: (UIImageView *) imageView
{
UIImage *rotatedImage;

// Get image width, height of the bounding rectangle
CGRect boundingRect = [self getBoundingRectAfterRotation: imageView.bounds byAngle:angle];

// Create a graphics context the size of the bounding rectangle
UIGraphicsBeginImageContext(boundingRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

// Rotate and translate the context
CGAffineTransform ourTransform = CGAffineTransformIdentity;
ourTransform = CGAffineTransformConcat(ourTransform, CGAffineTransformMakeRotation(angle));

CGContextConcatCTM(context, ourTransform);

// Draw the image into the context
CGContextDrawImage(context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);

// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];

// Clean up
UIGraphicsEndImageContext();
return rotatedImage;
}

然而,图像不会围绕其中心旋转。我尝试了各种与我的旋转连接的变换以使其围绕中心旋转,但无济于事。我错过了一个技巧吗?由于我旋转的是上下文而不是图像,这甚至可能吗?

现在急于完成这项工作,因此将不胜感激。

戴夫

编辑 :我多次被问到我的 boundingRect 代码,所以这里是:
- (CGRect) getBoundingRectAfterRotation: (CGRect) rectangle byAngle: (CGFloat) angleOfRotation {
// Calculate the width and height of the bounding rectangle using basic trig
CGFloat newWidth = rectangle.size.width * fabs(cosf(angleOfRotation)) + rectangle.size.height * fabs(sinf(angleOfRotation));
CGFloat newHeight = rectangle.size.height * fabs(cosf(angleOfRotation)) + rectangle.size.width * fabs(sinf(angleOfRotation));

// Calculate the position of the origin
CGFloat newX = rectangle.origin.x + ((rectangle.size.width - newWidth) / 2);
CGFloat newY = rectangle.origin.y + ((rectangle.size.height - newHeight) / 2);

// Return the rectangle
return CGRectMake(newX, newY, newWidth, newHeight);
}

最佳答案

好的 - 最后我似乎已经做到了。任何关于正确性的评论都是有用的……需要平移、旋转、缩放和与绘图矩形位置的偏移才能使其工作。代码在这里:

CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, boundingRect.size.width/2, boundingRect.size.height/2);
transform = CGAffineTransformRotate(transform, angle);
transform = CGAffineTransformScale(transform, 1.0, -1.0);

CGContextConcatCTM(context, transform);

// Draw the image into the context
CGContextDrawImage(context, CGRectMake(-imageView.image.size.width/2, -imageView.image.size.height/2, imageView.image.size.width, imageView.image.size.height), imageView.image.CGImage);

// Get an image from the context
rotatedImage = [UIImage imageWithCGImage: CGBitmapContextCreateImage(context)];

关于uiimageview - 从旋转的 UIImageView 创建 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2856556/

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