旋转UIImage时避免内存峰值

5

我目前正在使用CGContextDrawImage旋转图像,但每次调用时都会出现巨大的内存消耗。新设备可以处理它,但像iPhone 4s这样RAM较低的设备无法处理。该图像是用户以AVCaptureSessionPresetHigh拍摄的大文件。

我目前正在使用UIImage扩展来旋转图像。

func rotate(orientation: UIImageOrientation) -> UIImage{
    if(orientation == UIImageOrientation.Up){return self}

    var transform: CGAffineTransform = CGAffineTransformIdentity

    if(orientation == UIImageOrientation.Down || orientation == UIImageOrientation.DownMirrored){
        transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height)
        transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
    }
    else if(orientation == UIImageOrientation.Left || orientation == UIImageOrientation.LeftMirrored){
        transform = CGAffineTransformTranslate(transform, self.size.width, 0)
        transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
    }
    else if(orientation == UIImageOrientation.Right || orientation == UIImageOrientation.RightMirrored){
        transform = CGAffineTransformTranslate(transform, 0, self.size.height)
        transform = CGAffineTransformRotate(transform,  CGFloat(-M_PI_2))
    }


    if(orientation == UIImageOrientation.UpMirrored || orientation == UIImageOrientation.DownMirrored){
        transform = CGAffineTransformTranslate(transform, self.size.width, 0)
        transform = CGAffineTransformScale(transform, -1, 1)
    }
    else if(orientation == UIImageOrientation.LeftMirrored || orientation == UIImageOrientation.RightMirrored){
        transform = CGAffineTransformTranslate(transform, self.size.height, 0)
        transform = CGAffineTransformScale(transform, -1, 1);
    }

    let ref: CGContextRef = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage))
    CGContextConcatCTM(ref, transform)

    if(orientation == UIImageOrientation.Left || orientation == UIImageOrientation.LeftMirrored || orientation == UIImageOrientation.Right || orientation == UIImageOrientation.RightMirrored){
        CGContextDrawImage(ref, CGRectMake(0, 0, self.size.height, self.size.width), self.CGImage)
    }
    else{
        CGContextDrawImage(ref, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage)
    }


    let cgImg: CGImageRef = CGBitmapContextCreateImage(ref)
    let rotated: UIImage = UIImage(CGImage: cgImg)!

    return rotated
}

这段代码在像iPhone 5和6这样的设备上完美运行,但几乎总是导致iPhone 4s由于低内存而崩溃。

我知道我可以通过上传指定方向的EXIF数据来解决问题,但我认为在上传之前旋转图像会在以后防止更多问题。

iOS如何使用EXIF数据旋转图像进行显示?例如,尽管使用

UIImage(image.CGImage, scale: 1.0, orientation: UIImageOrientation.Right)

更改 EXIF 数据后,图像在屏幕上仍以正确的方向显示,同时内存占用更少。是否有一些可以使用 GPU 的低级代码来实现这一点,就像上面的代码所做的那样?

我还想避免在上传之前缩小图像。

是否有任何减少旋转图像影响的方法,例如分别旋转它的不同部分,或者我必须找到另一种替代方法?


你有什么图像尺寸? - John Tracid
@JohnTracid 这是用户在 AVCaptureSessionPresetHigh 下拍摄的照片。 - Jojodmo
1个回答

2

我能够(大部分)通过在上述代码的结尾调用UIGraphicsEndImageContext(),并且直到上传之前才旋转图像(用户可以在上传之前旋转他们的照片,因此直到上传前不旋转它们可以极大地减少内存使用)来解决这个问题。

因此,每当用户旋转一张照片时,我调用了这个我创建的rotateExif(orientation)函数。

extension UIImage{
    func rotateExif(orientation: UIImageOrientation) -> UIImage{

        if(orientation == UIImageOrientation.Up){return self}

        let current = self.imageOrientation
        let currentDegrees: Int = (
            current == UIImageOrientation.Down || current == UIImageOrientation.DownMirrored ? 180 : (
                current == UIImageOrientation.Left || current == UIImageOrientation.LeftMirrored ? 270 : (
                    current == UIImageOrientation.Right || current == UIImageOrientation.RightMirrored ? 90 : 0
                )
            )
        )
        let changeDegrees: Int = (
            orientation == UIImageOrientation.Down || orientation == UIImageOrientation.DownMirrored ? 180 : (
                orientation == UIImageOrientation.Left || orientation == UIImageOrientation.LeftMirrored ? 270 : (
                    orientation == UIImageOrientation.Right || orientation == UIImageOrientation.RightMirrored ? 90 : 0
                )
            )
        )


        let mirrored: Bool = (
            current == UIImageOrientation.DownMirrored || current == UIImageOrientation.UpMirrored ||
            current == UIImageOrientation.LeftMirrored || current == UIImageOrientation.RightMirrored ||
            orientation == UIImageOrientation.DownMirrored || orientation == UIImageOrientation.UpMirrored ||
            orientation == UIImageOrientation.LeftMirrored || orientation == UIImageOrientation.RightMirrored
        )

        let degrees: Int = currentDegrees + changeDegrees

        let newOrientation: UIImageOrientation = (
            degrees == 270 || degrees == 630 ? (mirrored ? UIImageOrientation.LeftMirrored : UIImageOrientation.Left) : (
                degrees == 180 || degrees == 540 ? (mirrored ? UIImageOrientation.DownMirrored : UIImageOrientation.Down) : (
                    degrees == 90 || degrees == 450 ? (mirrored ? UIImageOrientation.RightMirrored : UIImageOrientation.Right) : (
                        mirrored ? UIImageOrientation.UpMirrored : UIImageOrientation.Up
                    )
                )
            )
        )

        return UIImage(CGImage: self.CGImage!, scale: 1.0, orientation: newOrientation)
    }
}

这会根据输入的orientation旋转图像的EXIF信息。例如,如果原始方向为Right,并且被旋转了Right,则新方向将是Down。如果原始方向为RightMirrored,并且被旋转了Left,新方向将是UpMirrored

接下来,在上传照片之前,通过在UIImage上调用问题中的代码来旋转实际像素:image.rotate(image.imageOrientation)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接