4

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

3
  • imageData is already a binary representation of your image. What is your problem? Commented Nov 11, 2015 at 9:33
  • can i send this binary to server as multipart ? Commented Nov 11, 2015 at 18:22
  • ImageDisplay.image.pngData() Commented Jul 14, 2022 at 11:53

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

2 Comments

let imgTest = UIImage.init(named: "logo.png") let imageData = imgTest!.pngData() Latest Code
let img = UIImage(named: "response") let data = img?.pngData() worked for me! just to share.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.