Murtada Altaee
Murtada Altaee

Reputation: 49

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))

Upvotes: 4

Views: 7498

Answers (1)

t4nhpt
t4nhpt

Reputation: 5302

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)

Upvotes: 6

Related Questions