Murtada Altaee Murtada Altaee - 6 months ago 30
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)

print(imageData)


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

Answer

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)

print(imageData)