Murtada Altaee Murtada Altaee - 2 years ago 136
Swift Question

How to convert image to binary in Swift

I created an app to take an image and convert this image to binary and send to server. I take the image but I can't convert it.

I use this code :

func cameraa(){
let picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .Camera
presentViewController(picker, animated: true, completion: nil)

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
ImageDisplay.image = info[UIImagePickerControllerOriginalImage] as? UIImage;dismissViewControllerAnimated(true, completion: nil)

@IBAction func Encode(sender: UIButton) {

var imageEncode = ImageDisplay.image

let image : UIImage = UIImage(imageEncode)
let imageData = UIImagePNGRepresentation(image)


My error in parse image(imageEncode) to (let image : UIImage = UIImage(imageEncode))

Answer Source

The ImageDisplay.image is already a UIImage. So you needn't to convert it to UIImage again. Just do that:

let imageData = UIImagePNGRepresentation(ImageDisplay.image)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download