Python 从iOS设备发送的字符串创建图像
我有一个iOS移动应用程序,它将编码图像发送到Python3服务器Python 从iOS设备发送的字符串创建图像,python,ios,python-3.x,Python,Ios,Python 3.x,我有一个iOS移动应用程序,它将编码图像发送到Python3服务器 static func prepareImageAndUpload(imageView: UIImageView) -> String? { if let image: UIImage? = imageView.image { // You create a NSData from your image let imageData = UIImageJPEGRepresentatio
static func prepareImageAndUpload(imageView: UIImageView) -> String?
{
if let image: UIImage? = imageView.image {
// You create a NSData from your image
let imageData = UIImageJPEGRepresentation(imageView.image!, 0.5)
// You create a base64 string
let base64String = imageData!.base64EncodedStringWithOptions([])
// And you encode it in order to delete any problem of specials char
let encodeImg = base64String.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) as String!
return encodeImg
}
return nil
}
我正在尝试使用以下代码接收该图像:
imageName = "imageToSave.jpg"
fh = open(imageName, "wb")
imgDataBytes = bytes(imgData, encoding="ascii")
imgDataBytesDecoded = base64.b64decode(imgDataBytes)
fh.write(imgDataBytesDecoded)
fh.close()
我成功创建了图像文件,没有任何中断。我可以看到文件大小是正确的,但是图像是不正确的,因为它无法打开并且显示它已损坏
我不确定错误在哪里,因为逻辑如下:
为什么在发送到服务器之前转换为字符串?您不能确定文件是否已损坏或有效。发送到服务器原始图像(文件上载)并忽略所有
EOF
错误。(附加:`python-u`(二进制模式))我试图删除url编码:let encodeImg=base64String.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())作为字符串代码>和它仍然是一样的。这就是你在这里的意思吗?允许的字符集总是返回损坏的图像字符串。将bzip2与jqeryajax一起使用,将pic发送到服务器。服务器响应压缩(bz2)映像。编码器和解码器的类型不同。丢失数据包,断开连接,我不知道如何在iOS应用程序中使用jquery ajax。需要在web浏览器上运行(适用于所有兼容操作系统)。谁可以信任您的应用程序以使用更多权限运行。Web应用程序提供了更大的灵活性。我创建一个exe(适用于windows)时,总是会有125毫秒的延迟。将scada应用程序转换为webapp,我现在的响应为0.87毫秒。如果不使用webapp,跨平台总是一个问题。