1

I simply want to lower the quality or shrink the image so the "parse" will accept the image once I hit the button compose.

Exact error is

reason: 'PFFile cannot be larger than 10485760 bytes'

This is my code:

import UIKit
import Parse
import Bolts
import ParseUI

class ComposeViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate {

@IBOutlet weak var captionTextView: UITextView!
@IBOutlet weak var previewImage: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    captionTextView.delegate = self 

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

}

@IBAction func addImageTapped(sender: AnyObject) {
    let imagePicker = UIImagePickerController()

    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.mediaTypes = UIImagePickerController.availableMediaTypesForSourceType(.PhotoLibrary)!
    imagePicker.allowsEditing = false

    self.presentViewController(imagePicker, animated: true, completion: nil)
}
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {

        self.previewImage.image = image

        self.dismissViewControllerAnimated(true, completion: nil)

    }
func textViewShouldEndEditing(textView: UITextView) -> Bool {
    captionTextView.resignFirstResponder()
    return true;
}
@IBAction func composeTapped(sender: AnyObject) {


    let date = NSDate()
    let dateFormatter = NSDateFormatter()
    dateFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
    let localDate = dateFormatter.stringFromDate(date)

    let imageToBeUploaded = self.previewImage.image
    let imageData = UIImagePNGRepresentation(imageToBeUploaded)



    let file: PFFile = PFFile(data: imageData)
    let fileCaption: String = self.captionTextView.text

    var photoToUpload = PFObject(className: "Posts")
    photoToUpload["Image"] = file
    photoToUpload["Caption"] = fileCaption
    photoToUpload["addedBy"] = PFUser.currentUser()?.username
    photoToUpload["date"] = localDate

    photoToUpload.save()

    println("Successfully Posted.")


}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

}
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
Hunter
  • 1,321
  • 4
  • 18
  • 34
  • Can you make a minimal example, if you really need to put code here. Also, there's bound to be a lot of results if you just google *swift image compression*. – Marcus Müller Aug 30 '15 at 06:57
  • You might want to take a look at this one also http://stackoverflow.com/a/29726675/2303865 – Leo Dabus Aug 30 '15 at 08:01

0 Answers0