Cómo rotar un UIImage 90 grados?

tengo unUIImage es decirUIImageOrientationUp (retrato) que me gustaría rotar en sentido contrario a las agujas del reloj en 90 grados (hacia el paisaje). No quiero usar unCGAffineTransform. Quiero los píxeles de laUIImage para cambiar realmente la posición. Estoy utilizando un bloque de código (que se muestra a continuación) originalmente destinado a cambiar el tamaño de unUIImage para hacer esto. Establecí un tamaño objetivo como el tamaño actual de laUIImage pero me sale un error:

(Error): CGBitmapContextCreate: bytes de datos no válidos / fila: debe ser al menos 1708 para 8 bits / componentes enteros, 3 componentes, kCGImageAlphaPremultipliedLast.

(No recibo un error cada vez que proporciono un tamaño MÁS PEQUEÑO como el tamaño objetivo BTW). Como puedo rotar miUIImage 90 grados CCW utilizando solo funciones de gráficos centrales, al tiempo que conserva el tamaño actual?

-(UIImage*)reverseImageByScalingToSize:(CGSize)targetSize:(UIImage*)anImage
{
    UIImage* sourceImage = anImage; 
    CGFloat targetWidth = targetSize.height;
    CGFloat targetHeight = targetSize.width;

    CGImageRef imageRef = [sourceImage CGImage];
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone) {
        bitmapInfo = kCGImageAlphaNoneSkipLast;
    }

    CGContextRef bitmap;

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {


        bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    }       


    if (sourceImage.imageOrientation == UIImageOrientationRight) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -targetHeight);

    } else if (sourceImage.imageOrientation == UIImageOrientationLeft) {
        CGContextRotateCTM (bitmap, radians(-90));
        CGContextTranslateCTM (bitmap, -targetWidth, 0);

    } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
        // NOTHING
    } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
        CGContextRotateCTM (bitmap, radians(90));
        CGContextTranslateCTM (bitmap, 0, -targetHeight);
    }

    CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    return newImage; 
}

Respuestas a la pregunta(19)

Su respuesta a la pregunta