2015-09-14 56 views
4

我目前正在使用CGContextDrawImage旋转图像,但每当它被调用时都会有一个巨大的内存尖峰。较新的设备可以处理它,但具有较低RAM的设备(如iPhone 4s)则不能。该图像是UIImage旋转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 
} 

此代码工作完全在设备由用户在AVCaptureSessionPresetHigh

我目前正在旋转使用此扩展的图像拍摄一个大的文件,如iPhone 5和6,但几乎总是会导致iPhone 4s因内存不足而崩溃。

我知道我可以用EXIF数据指定方向上载图像,但我觉得只是在上传图像之前旋转图像可以防止进一步的问题。

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

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

改变EXIF数据,图像仍然显示在屏幕上的正确取向,与存储器的影响少了很多。是否有一些真正的低级代码可以像上面的代码一样使用GPU来实现这一点?

我还想避免在上传图片前缩放图片。

有没有什么办法可以减少旋转图像的影响,比如旋转它,或者我将不得不寻找替代物?

+0

你有什么图像大小? –

+0

@JohnTracid这是用户在'AVCaptureSessionPresetHigh'拍摄的照片 – Jojodmo

回答

2

我能够(主要)通过在上面的代码末尾调用UIGraphicsEndImageContext()来解决这个问题,以及在上传图片之前不旋转图片(用户可以在上传图片前旋转它们,所以不能旋转直到上传之前它们大大减少了内存使用量)。

所以,当用户旋转的照片,我把我创建

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这个rotateExif(orientation)功能。因此,例如,如果原始方向是Right,并且它被旋转了Right,则新方向将是Down。如果原件是RightMirrored,并且它被旋转了Left,则新定位将是UpMirrored

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