Jak obrócić obraz UIImage o 90 stopni?

mamUIImage to jestUIImageOrientationUp (portret), który chciałbym obrócić w lewo o 90 stopni (do krajobrazu). Nie chcę używaćCGAffineTransform. Chcę pikseleUIImage faktycznie zmienić pozycję. Używam bloku kodu (pokazanego poniżej) pierwotnie przeznaczonego do zmiany rozmiaruUIImage aby to zrobić. Ustawiam rozmiar docelowy jako bieżący rozmiarUIImage ale pojawia się błąd:

(Błąd): CGBitmapContextCreate: niepoprawne bajty danych / wiersz: powinien wynosić co najmniej 1708 dla 8 bitów całkowitych / komponentu, 3 komponentów, kCGImageAlphaPremultipliedLast.

(Nie dostaję błędu za każdym razem, gdy podam rozmiar MNIEJSZY jako rozmiar docelowy BTW). Jak mogę OBRÓCIĆ mójUIImage 90 stopni w lewo przy użyciu podstawowych funkcji graficznych przy zachowaniu obecnego rozmiaru?

-(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; 
}

questionAnswers(19)

yourAnswerToTheQuestion