iOS UIImagePickerController result image orientation after upload

后端 未结 20 1974
长发绾君心
长发绾君心 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:02

    @an0, thanks for the answer!
    The only thing is autoreleasepool: 
    
    func fixOrientation(img: UIImage) -> UIImage? {
    
        let result: UIImage?
        if img.imageOrientation == .up {
            result = img
        } else {
    
            result = autoreleasepool { () -> UIImage? in
                UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale)
                let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
                img.draw(in: rect)
    
                let normalizedImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
    
                return normalizedImage
            }
        }
    
        return result
    }
    

提交回复
热议问题