【问题标题】:Creating a UIImage from a rotated UIImageView从旋转的 UIImageView 创建 UIImage
【发布时间】:2010-05-18 11:00:29
【问题描述】:

我有一个 UIImageView,里面有一张图片。我通过将 UIImageView 的变换属性设置为 CGAffineTransformMakeRotation(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;
 }

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

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

戴夫

编辑:我的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);
}

【问题讨论】:

  • 这是什么getBoundingRectAfterRotation函数?
  • 嗨@bmdhacks getBoundingRectAfterRotation 是我创建的一个方法,所以你不会在任何Cocoa-Touch 类中找到它。它接受一个矩形和一个角度作为参数,并返回一个新的 CGRect,如果它被旋转给定的角度,它将包含原始矩形。
  • 嗨@bmdhacks 由于受欢迎的需求,我在原始问题的编辑中添加了该方法。

标签: uiimageview uiimage rotation graphicscontext


【解决方案1】:

好的——我似乎终于做到了。任何关于正确性的 cmets 都会很有用……需要平移、旋转、缩放和从绘图矩形位置偏移才能使其工作。代码在这里:

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)];

【讨论】:

  • 为什么需要transform = CGAffineTransformScale(transform, 1.0, -1.0);
  • CoreGraphics 使用坐标系,原点位于左下角。 UIKit 对象使用左上角的原点,y 轴随着您向下移动屏幕而增加。此变换翻转 y 轴以确保图像以正确的方式向上绘制。
  • 谢谢!我一直在努力通过平移、旋转和缩放在上下文中绘制图像。我尝试了 UIImage 的 drawInRect: 并翻译上下文,但它总是围绕左上角 (0,0) 旋转。您的解决方案是唯一有效的解决方案! :)
  • 您好,此代码不适用于与 UIImageOrientationUp 方向不同的照片,您有什么解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
  • 2018-08-14
相关资源
最近更新 更多