-3

I am calling to all swift developers here. I am struggling last 3 days with this topic. Is there a way to pick an image from image picker (local photo library of iPhone) and upload it to server? I can't figure out how to do this....just tons of outdated code or nothin.

Pan Mluvčí
  • 1,242
  • 2
  • 21
  • 42
  • You need to do some research before posting questions like this, there is plenty of documentation on the web and on Apple Developer site for learning coding. – RichAppz Nov 12 '15 at 11:04

1 Answers1

0

You should try to use a UIImagePickerController like this

@IBAction func takePhoto(sender: UIButton) {

    let picker = UIImagePickerController()
    picker.delegate = self;
    picker.allowsEditing = true;
    picker.sourceType = .PhotoLibrary;

    self.presentViewController(picker animated:true completion:nil); 
}

Then you can get the image in imagePickerController(_:didFinishPickingMediaWithInfo:) method from UIImagePickerControllerDelegate

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
    UploadImage(chosenImage)

    picker.dismissViewControllerAnimated(true, completion: nil);
}

func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    picker.dismissViewControllerAnimated(true, completion: nil);
}

Finally you upload the image. Example using HTTP POST Check this answer

function UploadImage(image : UIImage) {
    var imageData = UIImagePNGRepresentation(image)
    if imageData == nil {
        print("image not valid")
        return
    }

    // Upload
    ....
}

Note This answer is directly written in the reply, please change any invalid code.

Community
  • 1
  • 1
zc246
  • 1,514
  • 16
  • 28