旋转CGImage

时间:2012-05-11 03:06:02

标签: objective-c ios uiimage cgimage

我有一个UIImage,我从UIImagePickerController获取。当我从- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法接收图像时,我将其直接放入UIImageView进行预览,并为用户提供保存或丢弃图像的选项。

The preview screen

当用户选择保存选项时,应用程序获取图像的png数据并将其保存到文档目录中。但是在2种可能的设备方向之一(仅景观)下发生的事情是图像被颠倒保存(更具体地说,从它应该的位置旋转180度)。因此,当我在图库中加载图像时,它会显示为颠倒。

(参见图库中的左下角图片)

See the bottom left image

我已经解决了UIImage来自UIImage的UIImage中的原始图像数据没有旋转,而是将方向存储为对象的属性,仅在显示时应用。所以我试图使用我在网上找到的一些代码来旋转与UIImage相关联的CGImage。但它似乎对图像完全没有影响。我正在使用的代码如下:

- (void)rotateImage:(UIImage*)image byRadians:(CGFloat)rads
{   
    // calculate the size of the rotated view's containing box for our drawing space 
    UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
    CGAffineTransform t = CGAffineTransformMakeRotation(rads);
    rotatedViewBox.transform = t;
    CGSize rotatedSize = rotatedViewBox.frame.size;

    // Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize);
    CGContextRef bitmap = UIGraphicsGetCurrentContext();

    // Move the origin to the middle of the image you want to rotate and scale around the center. 
    CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); 

    // Rotate the image context
    CGContextRotateCTM(bitmap, rads);

    // Now, draw the rotated/scaled image into the context
    CGContextScaleCTM(bitmap, 1.0, -1.0);
    CGContextDrawImage(bitmap, CGRectMake(image.size.width / 2, image.size.height / 2, image.size.width, image.size.height), [image CGImage]);

    image = UIGraphicsGetImageFromCurrentImageContext();
    image = [UIImage imageWithCGImage:image.CGImage scale:1.0 orientation:UIImageOrientationDown];
    UIGraphicsEndImageContext();
}

我想知道为什么这段代码不能正常工作,因为我在网上找到的每一段代码都不能正常工作。

6 个答案:

答案 0 :(得分:25)

-(UIImage*) rotate:(UIImage*) src andOrientation:(UIImageOrientation)orientation
{
    UIGraphicsBeginImageContext(src.size);

    CGContextRef context=(UIGraphicsGetCurrentContext());

    if (orientation == UIImageOrientationRight) {
        CGContextRotateCTM (context, 90/180*M_PI) ;
    } else if (orientation == UIImageOrientationLeft) {
        CGContextRotateCTM (context, -90/180*M_PI);
    } else if (orientation == UIImageOrientationDown) {
        // NOTHING
    } else if (orientation == UIImageOrientationUp) {
        CGContextRotateCTM (context, 90/180*M_PI);
    }

    [src drawAtPoint:CGPointMake(0, 0)];
    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;

}

使用这个......它完美无缺!从UIImagePicker中选择图像时,请确保使用此功能:

示例:

UIImage *image;
image = [self rotate:image andOrientation:image.imageOrientation];

确保在从文档目录中选择照片的位置使用此功能。

答案 1 :(得分:8)

旋转和镜像UIImage CGImage支持数据 - Swift

我最近想要更正由UIImage支持的CGImage以匹配所需的方向,而不是依赖API来尊重UIImageOrientation参数。

func createMatchingBackingDataWithImage(imageRef: CGImage?, orienation: UIImageOrientation) -> CGImage?
{
    var orientedImage: CGImage?

    if let imageRef = imageRef {
        let originalWidth = imageRef.width
        let originalHeight = imageRef.height
        let bitsPerComponent = imageRef.bitsPerComponent
        let bytesPerRow = imageRef.bytesPerRow

        let bitmapInfo = imageRef.bitmapInfo

        guard let colorSpace = imageRef.colorSpace else {
            return nil
        }

        var degreesToRotate: Double
        var swapWidthHeight: Bool
        var mirrored: Bool
        switch orienation {
        case .up:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = false
            break
        case .upMirrored:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = true
            break
        case .right:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .rightMirrored:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = true
            break
        case .down:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = false
            break
        case .downMirrored:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = true
            break
        case .left:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .leftMirrored:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = true
            break
        }
        let radians = degreesToRotate * Double.pi / 180.0

        var width: Int
        var height: Int
        if swapWidthHeight {
            width = originalHeight
            height = originalWidth
        } else {
            width = originalWidth
            height = originalHeight
        }

        let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)
        contextRef?.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)
        if mirrored {
            contextRef?.scaleBy(x: -1.0, y: 1.0)
        }
        contextRef?.rotate(by: CGFloat(radians))
        if swapWidthHeight {
            contextRef?.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
        } else {
            contextRef?.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
        }
        contextRef?.draw(imageRef, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(originalWidth), height: CGFloat(originalHeight)))
        orientedImage = contextRef?.makeImage()
    }

    return orientedImage
}

答案 2 :(得分:3)

我遇到了你的问题。你必须使用CGContextTranslateCTM(context, -rotatedSize.width/2, -rotatedSize.height/2);翻译上下文,并在绘图中将rect的原点设置为rotatingViewBox.frame.origin.x,rotatingViewBox.frame.origin.y。 使用此代码。

UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)];
CGAffineTransform t = CGAffineTransformMakeRotation(0.3);
rotatedViewBox.transform = t;
CGSize rotatedSize = rotatedViewBox.frame.size;



UIGraphicsBeginImageContext(rotatedSize);

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM (context, 0.3);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, -rotatedSize.width/2, -rotatedSize.height/2);




CGContextDrawImage(context, CGRectMake(-rotatedViewBox.frame.origin.x, -rotatedViewBox.frame.origin.y, image.size.width, image.size.height), [image CGImage]);


UIImage *nn = UIGraphicsGetImageFromCurrentImageContext();

NSLog(@"size is %f, %f",nn.size.width,nn.size.height);
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(nn, nil,nil,nil);

答案 3 :(得分:2)

Cameron Lowell Palmer answer Swift 3 版本:

func createMatchingBackingDataWithImage(imageRef: CGImage?, orienation: UIImageOrientation) -> CGImage? {
    var orientedImage: CGImage?

    if let imageRef = imageRef {
        let originalWidth = imageRef.width
        let originalHeight = imageRef.height
        let bitsPerComponent = imageRef.bitsPerComponent
        let bytesPerRow = imageRef.bytesPerRow

        let colorSpace = imageRef.colorSpace
        let bitmapInfo = imageRef.bitmapInfo

        var degreesToRotate: Double
        var swapWidthHeight: Bool
        var mirrored: Bool
        switch orienation {
        case .up:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = false
            break
        case .upMirrored:
            degreesToRotate = 0.0
            swapWidthHeight = false
            mirrored = true
            break
        case .right:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .rightMirrored:
            degreesToRotate = 90.0
            swapWidthHeight = true
            mirrored = true
            break
        case .down:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = false
            break
        case .downMirrored:
            degreesToRotate = 180.0
            swapWidthHeight = false
            mirrored = true
            break
        case .left:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = false
            break
        case .leftMirrored:
            degreesToRotate = -90.0
            swapWidthHeight = true
            mirrored = true
            break
        }
        let radians = degreesToRotate * Double.pi / 180

        var width: Int
        var height: Int
        if swapWidthHeight {
            width = originalHeight
            height = originalWidth
        } else {
            width = originalWidth
            height = originalHeight
        }

        if let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace!, bitmapInfo: bitmapInfo.rawValue) {

            contextRef.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)
            if mirrored {
                contextRef.scaleBy(x: -1.0, y: 1.0)
            }
            contextRef.rotate(by: CGFloat(radians))
            if swapWidthHeight {
                contextRef.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
            } else {
                contextRef.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
            }
            contextRef.draw(imageRef, in: CGRect(x: 0, y: 0, width: originalWidth, height: originalHeight))

            orientedImage = contextRef.makeImage()
        }
    }

    return orientedImage
}

用法:

let newCgImage = createMatchingBackingDataWithImage(imageRef: cgImageRef, orienation: .left)

答案 4 :(得分:1)

这只是重构先前的答案。

a

答案 5 :(得分:0)

所有这些答案的问题在于它们具有 UIKit 依赖项。我正在开发针对 iOS 和 macOS 的 SwiftUI 通用应用程序。 Cameron Lowell Palmer answer 进行了一些修改:

  1. bytesPerRow 的计算需要在高度和宽度的交换之后进行,否则 CGImageContext 抱怨 bytesPerRow 不正确

  2. 将 UIImageOrientation 更改为 CGImagePropertyOrientation,并交换左右旋转的符号

这是我的版本,适用于 iOS 和 macOS:

extension CGImage {
func rotating(to orientation: CGImagePropertyOrientation) -> CGImage? {
    var orientedImage: CGImage?

    let originalWidth = self.width
    let originalHeight = self.height
    let bitsPerComponent = self.bitsPerComponent
    let bitmapInfo = self.bitmapInfo

    guard let colorSpace = self.colorSpace else {
        return nil
    }

    var degreesToRotate: Double
    var swapWidthHeight: Bool
    var mirrored: Bool

    switch orientation {
    case .up:
        degreesToRotate = 0.0
        swapWidthHeight = false
        mirrored = false
        break
    case .upMirrored:
        degreesToRotate = 0.0
        swapWidthHeight = false
        mirrored = true
        break
    case .right:
        degreesToRotate = -90.0
        swapWidthHeight = true
        mirrored = false
        break
    case .rightMirrored:
        degreesToRotate = -90.0
        swapWidthHeight = true
        mirrored = true
        break
    case .down:
        degreesToRotate = 180.0
        swapWidthHeight = false
        mirrored = false
        break
    case .downMirrored:
        degreesToRotate = 180.0
        swapWidthHeight = false
        mirrored = true
        break
    case .left:
        degreesToRotate = 90.0
        swapWidthHeight = true
        mirrored = false
        break
    case .leftMirrored:
        degreesToRotate = 90.0
        swapWidthHeight = true
        mirrored = true
        break
    }

    let radians = degreesToRotate * Double.pi / 180.0

    var width: Int
    var height: Int

    if swapWidthHeight {
        width = originalHeight
        height = originalWidth
    } else {
        width = originalWidth
        height = originalHeight
    }

    let bytesPerRow = (width * bitsPerPixel) / 8

    let contextRef = CGContext(data: nil, width: width, height: height, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

    contextRef?.translateBy(x: CGFloat(width) / 2.0, y: CGFloat(height) / 2.0)

    if mirrored {
        contextRef?.scaleBy(x: -1.0, y: 1.0)
    }

    contextRef?.rotate(by: CGFloat(radians))

    if swapWidthHeight {
        contextRef?.translateBy(x: -CGFloat(height) / 2.0, y: -CGFloat(width) / 2.0)
    } else {
        contextRef?.translateBy(x: -CGFloat(width) / 2.0, y: -CGFloat(height) / 2.0)
    }

    contextRef?.draw(self, in: CGRect(x: 0.0, y: 0.0, width: CGFloat(originalWidth), height: CGFloat(originalHeight)))

    orientedImage = contextRef?.makeImage()

    return orientedImage
}

}