在没有内存峰值的情况下旋转 UIImage

Posted

技术标签:

【中文标题】在没有内存峰值的情况下旋转 UIImage【英文标题】:Rotate a UIImage without memory spikes 【发布时间】:2015-09-14 05:43:18 【问题描述】:

我目前正在使用CGContextDrawImage 旋转图像,但每次调用它时都会出现巨大的内存峰值。较新的设备可以处理它,但内存较低的设备(例如 iPhone 4s)则不能。图片是用户在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 来实现这一点?

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

有什么方法可以减少旋转图像的影响,例如将其旋转成碎片,还是我必须找到替代方法?

【问题讨论】:

你的图片尺寸是多少? @JohnTracid 这是用户AVCaptureSessionPresetHigh拍摄的照片 【参考方案1】:

我能够(大部分)通过在上述代码末尾调用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)
    

这会将图像的 EXIF 旋转在orientation 中输入的量。因此,例如,如果原始方向是 Right,并且旋转了 Right,则新方向将是 Down。如果原来的方向是RightMirrored,然后旋转了Left,那么新的方向就是UpMirrored

然后,在上传照片之前,通过调用问题中的代码作为UIImageimage.rotate(image.imageOrientation) 的扩展名来旋转实际像素。

【讨论】:

以上是关于在没有内存峰值的情况下旋转 UIImage的主要内容,如果未能解决你的问题,请参考以下文章

二分法07:寻找峰值

c# windows app - 分析峰值内存使用情况并识别趋势

循环和便利方法是不是会导致 ARC 出现内存峰值?

如何在没有活动旋转的情况下旋转活动内的片段?

如何在没有聚合的情况下进行旋转?

matlab中的内存监控