简体   繁体   English

Swift UI 和 Alamofire 图片上传

[英]Swift UI and Alamofire Image Upload

I'm new to Swift and decided to try Alamofire for networking in my project.我是 Swift 的新手,并决定在我的项目中尝试使用 Alamofire 进行联网。 I've checked a lot of guides on the web, but most of them are outdated and use UIImage instead of Swift UI Image type.我检查了很多关于 web 的指南,但其中大多数都已过时,并使用 UIImage 而不是 Swift UI 图像类型。

Here is my code:这是我的代码:

@State var image: Image? = nil

.... ……

AF.upload(multipartFormData: { multipartFormData in
                multipartFormData.append(Data(self.name.utf8), withName: "name")
                multipartFormData.append(Data(self.price.utf8), withName: "price")
                multipartFormData.append(Data(self.image!), withName: "image")
            }, to: upload_url, headers: headers)

                .responseDecodable(of: WishModel.self) { response in
                    debugPrint(response)
                }

Apparently Data struct from Alamofire doesn't support this type.显然来自 Alamofire 的数据结构不支持这种类型。

Cannot invoke initializer for type 'Data' with an argument list of type '(Image)'

Also, I couldn't find any methods that can convert Image into Base64 type.另外,我找不到任何可以将 Image 转换为 Base64 类型的方法。 Is the only way to solve this issue to replace all Image instances into UIImage?解决此问题的唯一方法是将所有 Image 实例替换为 UIImage 吗?

Try converting the Image object to UIImage object, and you may do so by creating the UIImage from the Data itself.尝试将Image object 转换为UIImage object,您可以通过从Data本身创建UIImage来实现。

Now if you would like to get the Data from an Image view, you do not get it from the Image itself, but from the same place the Image got it.现在,如果您想从 Image 视图中获取Data ,则不是从Image本身获取数据,而是从Image获取数据的同一位置获取。 For example, if you load the Image using a uiImage object, you should go to that uiImage object. For example, if you load the Image using a uiImage object, you should go to that uiImage object. If the view got the image from a file name, you should go get it from the file.如果视图从文件名中获取图像,您应该 go 从文件中获取它。 You will never be able to interrogate Image for its binary, it is not the way the framework is designed.您将永远无法查询 Image 的二进制文件,这不是框架的设计方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM