title: Swift Code for reducing / compressing pictures saved to parse
body: <p>Please look at the code below if you believe there is anything that can be done to improve the swift code please let me know - I converted this code from objective-c off the raw engineering website <a href="http://www.raweng.com/blog/2013/03/04/improving-image-compression-what-weve-learned-from-whatsapp/" rel="nofollow">www.rawend.com</a></p>
  
  <p>@IBAction func signUp(sender: AnyObject) {</p>
  
  <pre><code>    let imageData = UIImagePNGRepresentation(self.profilePic.image)
  
      let image = UIImage(data: imageData)
  
      func compressImage(image:UIImage) -&gt; NSData {
          // Reducing file size to a 10th
  
          var actualHeight : CGFloat = image.size.height
          var actualWidth : CGFloat = image.size.width
          var maxHeight : CGFloat = 1136.0
          var maxWidth : CGFloat = 640.0
          var imgRatio : CGFloat = actualWidth/actualHeight
          var maxRatio : CGFloat = maxWidth/maxHeight
          var compressionQuality : CGFloat = 0.5
  
          if (actualHeight &gt; maxHeight || actualWidth &gt; maxWidth){
              if(imgRatio &lt; maxRatio){
                  //adjust width according to maxHeight
                  imgRatio = maxHeight / actualHeight;
                  actualWidth = imgRatio * actualWidth;
                  actualHeight = maxHeight;
              }
              else if(imgRatio &gt; maxRatio){
                  //adjust height according to maxWidth
                  imgRatio = maxWidth / actualWidth;
                  actualHeight = imgRatio * actualHeight;
                  actualWidth = maxWidth;
              }
              else{
                  actualHeight = maxHeight;
                  actualWidth = maxWidth;
                  compressionQuality = 1;
              }
          }
  
          var rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
          UIGraphicsBeginImageContext(rect.size);
          image.drawInRect(rect)
          var img = UIGraphicsGetImageFromCurrentImageContext();
          let imageData = UIImageJPEGRepresentation(img, compressionQuality);
          UIGraphicsEndImageContext();
  
          return imageData;
      }
  
      let compressedImage = compressImage(image!)
  
      let picture = PFFile(name: "image.png", data: compressedImage)
  
      PFUser.currentUser()!.setObject(picture, forKey: "profilePic")
      PFUser.currentUser()!.saveInBackgroundWithBlock {
          (success: Bool, error: NSError?) -&gt; Void in
          if (success) {
              println("image has been saved")
          } else {
              println("image failed")
          }
      }
  }
  </code></pre>