Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.09 KB | None | 0 0
  1. func resizeImageOriginalSize(target: CGSize) -> UIImage? {
  2.         let image = self
  3.         var num = self.getFileSize()
  4.         var newImager = UIImage()
  5.         var targetSize = target
  6.         var index = 0
  7.        
  8.         DispatchQueue.main.async {
  9.             NotificationCenter.default.post(
  10.                 name: .updateProgress,
  11.                 object: self,
  12.                 userInfo: ["Preparing to convert file.":  0.0])
  13.         }
  14.        
  15.         let incre = num / 500
  16.         var percent = 0.0
  17.         let orig = num
  18.        
  19.         while num > 500 {
  20.             index = index + 1
  21.             var actualHeight: Float = Float(image.size.height)
  22.             var actualWidth: Float = Float(image.size.width)
  23.             let maxHeight: Float = Float(targetSize.height)
  24.             let maxWidth: Float = Float(targetSize.width)
  25.             var imgRatio: Float = actualWidth / actualHeight
  26.             let maxRatio: Float = maxWidth / maxHeight
  27.             var compressionQuality: Float = 0.5
  28.             //50 percent compression
  29.            
  30.             if actualHeight > maxHeight || actualWidth > maxWidth {
  31.                 if imgRatio < maxRatio {
  32.                     //adjust width according to maxHeight
  33.                     imgRatio = maxHeight / actualHeight
  34.                     actualWidth = imgRatio * actualWidth
  35.                     actualHeight = maxHeight
  36.                 }
  37.                 else if imgRatio > maxRatio {
  38.                     //adjust height according to maxWidth
  39.                     imgRatio = maxWidth / actualWidth
  40.                     actualHeight = imgRatio * actualHeight
  41.                     actualWidth = maxWidth
  42.                 }
  43.                 else {
  44.                     actualHeight = maxHeight
  45.                     actualWidth = maxWidth
  46.                     compressionQuality = 1.0
  47.                 }
  48.             }
  49.            
  50.            
  51.             let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth), height: CGFloat(actualHeight))
  52.             UIGraphicsBeginImageContextWithOptions(rect.size, false, CGFloat(compressionQuality))
  53.             image.draw(in: rect)
  54.             let newImage = UIGraphicsGetImageFromCurrentImageContext()
  55.             UIGraphicsEndImageContext()
  56.             num = newImage!.getFileSize()
  57.             newImager = newImage!
  58.             print(num)
  59.             var w = targetSize.width - 1
  60.             var h = targetSize.height - 1
  61.             if index == 10 {
  62.                 let diff = orig - num
  63.                 let incree = diff / 10
  64.                 let needed = 500 / incree
  65.                 w = CGFloat(needed)
  66.                 h = CGFloat(needed)
  67.                
  68.             }
  69.          
  70.             targetSize = CGSize(width: w, height: h)
  71.             percent = Double(percent) + Double(incre)
  72.             DispatchQueue.main.async {
  73.                 NotificationCenter.default.post(
  74.                     name: .updateProgress,
  75.                     object: self,
  76.                     userInfo: ["CONVERT":  percent])
  77.             }
  78.            
  79.            
  80.         }
  81.         return newImager
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement