Создание UIImage из повернутого UIImageView

У меня есть UIImageView с изображением в нем. Я повернул изображение перед отображением, установив для свойства transform UIImageView значение 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;
 }

Однако изображение не вращается вокруг своего центра. Я попробовал все виды преобразований, связанных с моим вращением, чтобы заставить его вращаться вокруг центра, но безрезультатно. Я пропускаю трюк? Возможно ли это, поскольку я вращаю контекст, а не изображение?

Я отчаянно пытаюсь сделать эту работу сейчас, поэтому любая помощь будет принята с благодарностью.

Дейв

РЕДАКТИРОВАТЬМеня несколько раз спрашивали о моем коде 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);
}

Ответы на вопрос(1)

Ваш ответ на вопрос