iOS UIImagePickerController result image orientation after upload

后端 未结 20 1953
长发绾君心
长发绾君心 2020-11-22 00:44

I am testing my iPhone application on an iOS 3.1.3 iPhone. I am selecting/capturing an image using a UIImagePickerController:

UIImagePickerCont         


        
20条回答
  •  别跟我提以往
    2020-11-22 01:13

    Here is an UIImage extension in Swift 2 based on the accepted answer by @Anomie. It uses a clearer switch case. It also takes the optional value returned by CGBitmapContextCreateImage() into consideration.

    extension UIImage {
        func rotateImageByOrientation() -> UIImage {
            // No-op if the orientation is already correct
            guard self.imageOrientation != .Up else {
                return self
            }
    
            // We need to calculate the proper transformation to make the image upright.
            // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
            var transform = CGAffineTransformIdentity;
    
            switch (self.imageOrientation) {
            case .Down, .DownMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height)
                transform = CGAffineTransformRotate(transform, CGFloat(M_PI))
    
            case .Left, .LeftMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.width, 0)
                transform = CGAffineTransformRotate(transform, CGFloat(M_PI_2))
    
            case .Right, .RightMirrored:
                transform = CGAffineTransformTranslate(transform, 0, self.size.height)
                transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
    
            default:
                break
            }
    
            switch (self.imageOrientation) {
            case .UpMirrored, .DownMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.width, 0)
                transform = CGAffineTransformScale(transform, -1, 1)
    
            case .LeftMirrored, .RightMirrored:
                transform = CGAffineTransformTranslate(transform, self.size.height, 0)
                transform = CGAffineTransformScale(transform, -1, 1)
    
            default:
                break
            }
    
            // Now we draw the underlying CGImage into a new context, applying the transform
            // calculated above.
            let ctx = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height),
                CGImageGetBitsPerComponent(self.CGImage), 0,
                CGImageGetColorSpace(self.CGImage),
                CGImageGetBitmapInfo(self.CGImage).rawValue)
            CGContextConcatCTM(ctx, transform)
            switch (self.imageOrientation) {
            case .Left, .LeftMirrored, .Right, .RightMirrored:
                CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage)
    
            default:
                CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage)
            }
    
            // And now we just create a new UIImage from the drawing context
            if let cgImage = CGBitmapContextCreateImage(ctx) {
                return UIImage(CGImage: cgImage)
            } else {
                return self
            }
        }
    }
    

提交回复
热议问题