swift和python
I’m currently building an iOS app that requires some image processing functionality — the user would scan a food label, and I would need to parse the nutritional information and return some content back to the user.
我目前正在构建一个需要一些图像处理功能的iOS应用-用户将扫描食品标签,并且我需要解析营养信息并将一些内容返回给用户。
In this blog post, I’m going to explore how we can enable image transfers through a REST API — which I built with Python. I’m also going to briefly discuss how I interact with this API with Swift code within the iOS app.
在此博客文章中,我将探讨如何通过REST API(我使用Python构建)来启用图像传输。 我还将简要讨论如何在iOS应用中使用Swift代码与此API进行交互。
This is going to be part one of a two-part series. In this blog post, I go over my initial implementation of the client and API. In the next post, I’m going to explore another method for sending images through to the API. Let’s get started!
这将是一个分为两部分的系列之一。 在此博客文章中,我介绍了客户端和API的最初实现。 在下一篇文章中,我将探讨另一种将图像发送到API的方法。 让我们开始吧!
1 Base64编码 (1 Base64 Encoding)
Before we dive into a rough overview of the code, we need to think about how exactly we can transfer image data over the internet. You may be familiar with HTTP requests with JSON data — but this data is completely textual. How do we transfer visual content through textual data?
在深入了解代码之前,我们需要考虑如何精确地通过Internet传输图像数据。 您可能熟悉使用JSON数据的HTTP请求-但此数据完全是文本的。 我们如何通过文本数据传输视觉内容?
In my first implementation, I POST
JSON request data to my server endpoint. The JSON would have an image
property - which represents some textual representation of the image that we want to send.
在我第一次执行,我POST
JSON请求数据到我的服务器端点。 JSON具有image
属性-表示我们要发送的图像的某种文本表示形式。
To convert an image into a JSON-friendly format, we can use Base64 encoding. In short, Base64 converts a binary representation (an image file) into an ASCII string that is safe for plain text systems. The downside is that the resulting representation is ~1.3x larger in size than the original file. Keep this in mind when building the system — image file sizes can quickly add up, and that extra 33% of space might add up to be quite a lot over the long run!
要将图像转换为JSON友好格式,我们可以使用Base64编码 。 简而言之,Base64将二进制表示形式(图像文件)转换为对纯文本系统安全的ASCII字符串。 不利的一面是,结果表示的大小比原始文件大1.3倍。 在构建系统时,请记住这一点-图像文件的大小会Swift增加,从长远来看,额外的33%的空间可能会增加很多!
2基本的Python REST服务器 (2 Basic Python REST Server)
To implement a basic server, I used flask together with flask-restful. The docs are pretty self explanatory, so I will focus more on the specific implementation for image transfer.
为了实现基本服务器,我将flask和flask-restful一起使用。 这些文档很容易解释,因此我将重点放在图像传输的特定实现上。
With flask-restful, endpoints are defined as Python classes. In this example, we parse the image
argument from the POST
JSON request body.
在烧瓶稳定的情况下,端点被定义为Python类。 在此示例中,我们从POST
JSON请求正文中解析image
参数。
Simple enough! Now let’s take a look at the client side.
很简单! 现在,让我们看一下客户端。
3使用Swift发送图像(iOS) (3 Sending Images with Swift (iOS))
Let’s say that we want the user to take an image, then send it to our backend. In iOS, the output from the camera is of type AVCapturePhoto
. We can extract a Base64 encoded string like so:
假设我们希望用户拍摄图像,然后将其发送到我们的后端。 在iOS中,相机的输出为AVCapturePhoto
类型。 我们可以这样提取Base64编码的字符串:
To make requests to our backend server, we can use a library such as Alamofire. We can then create a service with a makeRequest
function:
要向后端服务器发出请求,我们可以使用Alamofire之类的库。 然后,我们可以使用makeRequest
函数创建服务:
Of course, in an actual app, we would have a callback that executes some action when the response comes back. This is a pretty simple system. I hope that it helps you make your own image transfer app :).
当然,在实际的应用中,我们将有一个回调,当响应返回时,该回调将执行某些操作。 这是一个非常简单的系统。 我希望它可以帮助您制作自己的图像传输应用程序:)。
翻译自: https://medium.com/swlh/restful-image-transfer-with-base64-encoding-cd9fb4453598
swift和python