Alişan Dağdelen Alişan Dağdelen - 7 months ago 17
Swift Question

How Can I Arrange Which Part of Code Work First

Im trying to update an image stores in parse.com . I have an imageview on my view and there is a default image on that imageview. I can choose photo from library and i can change my imageviews image with photo which i choose from library. But still my code saves default image to parse not the image which i choose from library.

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
profileImage.image = image
}

var objectIds = [String]()
@IBAction func changePp(sender: AnyObject) {
var image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image ,animated: true,completion: nil)

var queryU = PFQuery(className:"ProfilePhoto")
queryU.getObjectInBackgroundWithId(self.objectIds[0]) {
(Pp: PFObject?, error: NSError?) -> Void in
if error != nil {
print(error)
} else if let Pp = Pp {


let imageData = UIImagePNGRepresentation(self.profileImage.image!)
let imageFile = PFFile(name: "pImage.png",data: imageData!)
Pp["profilePhoto"] = imageFile


Pp.saveInBackground()
}
}


}


How can i provide saving to parse process after choose photo from library and set to my imageview ?

Answer

It is happening due to you are saving image on parse before to choose image from gallery. So please do it after picking the image from library.

func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        self.dismissViewControllerAnimated(true, completion: nil)
        profileImage.image = image


    var queryU = PFQuery(className:"ProfilePhoto")
        queryU.getObjectInBackgroundWithId(self.objectIds[0]) {
            (Pp: PFObject?, error: NSError?) -> Void in
            if error != nil {
                print(error)
            } else if let Pp = Pp {


                let imageData = UIImagePNGRepresentation(self.profileImage.image!)
                let imageFile = PFFile(name: "pImage.png",data: imageData!)
                    wait(w_status: 1000)
                Pp["profilePhoto"] = imageFile


                Pp.saveInBackground()
            }
        }
    }
Comments