从旋转的UIImageView创建UIImage

Mag*_*ave 15 rotation uiimageview uiimage graphicscontext

我有一个UIImageView,里面有一个图像.我通过将UIImageView的transform属性设置为CGAffineTransformMakeRotation(angle),在显示之前旋转了图像,其中angle是以弧度表示的角度.

我希望能够创建另一个UIImage,它对应于我在视图中可以看到的旋转版本.

我几乎在那里,通过旋转图像上下文我得到一个旋转的图像:

- (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;
 }
Run Code Online (Sandbox Code Playgroud)

但是图像不会围绕其中心旋转.我已经尝试了与我的旋转连接的各种变换,以使其围绕中心旋转,但无济于事.我错过了一招吗?这是否可能,因为我正在旋转上下文而不是图像?

现在急于让这项工作,所以任何帮助将不胜感激.

戴夫

编辑:我已经多次询问我的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);
}
Run Code Online (Sandbox Code Playgroud)

Mag*_*ave 22

好的 - 最后我似乎做到了.任何关于正确性的评论都是有用的......需要翻译,旋转,缩放和绘制矩形位置的偏移以使其工作.代码在这里:

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)];
Run Code Online (Sandbox Code Playgroud)

  • CoreGraphics使用坐标系统,其原点是左下角.当您向下移动屏幕时,UIKit对象使用左上角的原点,y轴增加.此变换翻转y轴以确保以正确的方式绘制图像. (3认同)