UIImage+Size.swift 830 B

123456789101112131415161718192021222324252627282930313233
  1. //
  2. // UIImage+Size.swift
  3. // Photo Editor
  4. //
  5. // Created by Mohamed Hamed on 5/2/17.
  6. // Copyright © 2017 Mohamed Hamed. All rights reserved.
  7. //
  8. import UIKit
  9. public extension UIImage {
  10. /**
  11. Suitable size for specific height or width to keep same image ratio
  12. */
  13. func suitableSize(heightLimit: CGFloat? = nil,
  14. widthLimit: CGFloat? = nil )-> CGSize? {
  15. if let height = heightLimit {
  16. let width = (height / self.size.height) * self.size.width
  17. return CGSize(width: width, height: height)
  18. }
  19. if let width = widthLimit {
  20. let height = (width / self.size.width) * self.size.height
  21. return CGSize(width: width, height: height)
  22. }
  23. return nil
  24. }
  25. }