- 
      
 - 
        
Save gorillka/a18a98e13eca4e46b127d9514a3f676b to your computer and use it in GitHub Desktop.  
    Resizing UIImage keeping aspect ratio
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import UIKit | |
| extension UIImage { | |
| func resized(maxSize: CGFloat) -> UIImage? { | |
| let scale: CGFloat | |
| if size.width > size.height { | |
| scale = maxSize / size.width | |
| } | |
| else { | |
| scale = maxSize / size.height | |
| } | |
| let newWidth = size.width * scale | |
| let newHeight = size.height * scale | |
| UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight)) | |
| draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight)) | |
| let newImage = UIGraphicsGetImageFromCurrentImageContext() | |
| UIGraphicsEndImageContext() | |
| return newImage | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment