如何在Swift中将图像转换为二进制

时间:2015-11-11 09:13:03

标签: swift

我创建了一个应用程序来获取图像并将此图像转换为二进制文件并发送到服务器。我拍摄的图片却无法转换。

我使用此代码:

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)

我在解析图片(imageEncode)中的错误(让图片:UIImage = UIImage(imageEncode))

1 个答案:

答案 0 :(得分:2)

ImageDisplay.image已经是UIImage。因此,您无需再次将其转换为UIImage。就这样做:

let imageData = UIImagePNGRepresentation(ImageDisplay.image)

print(imageData)