ios - Swift UI 和 Alamofire 图片上传

标签 ios swift swiftui alamofire alamofireimage

我是 Swift 的新手,并决定在我的项目中尝试使用 Alamofire 进行网络连接。我在网上查了很多指南,但大多数都过时了,并且使用 UIImage 而不是 Swift UI Image 类型。

这是我的代码:

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

显然来自 Alamofire 的数据结构不支持这种类型。
Cannot invoke initializer for type 'Data' with an argument list of type '(Image)'
另外,我找不到任何可以将 Image 转换为 Base64 类型的方法。解决此问题的唯一方法是将所有 Image 实例替换为 UIImage 吗?

最佳答案

尝试转换 Image反对UIImage对象,您可以通过创建 UIImage 来实现来自 Data本身。

现在,如果您想获得 Data从 ImageView 中,您无法从 Image 中获得它。本身,但来自同一个地方 Image知道了。例如,如果您使用 uiImage 对象加载 Image,则应该转到该 uiImage 对象。如果 View 从文件名中获取图像,则应该从文件中获取图像。您将永远无法查询 Image 的二进制文件,这不是框架的设计方式。

关于ios - Swift UI 和 Alamofire 图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61148207/

相关文章:

iphone - 不让 UIButton 拖到 Circle 外

iOS : Programmatically control the volume of Notification alert and sound theme

realm - SwiftUI 导航自动关闭/弹出 - Realm

ios - 如果先手动移动,则以编程方式移动 UITableViewCell 会失败

ios - QuickBlox-对话框必须在Objective-C中的内存缓存中

ios - 适用于 twilio 的 AlamoFire

Swift - 如果拖动则在内部执行触摸

html - 使用 SwiftUI 读取 HTML 字符串

swift - 我必须在 SwiftUI 中使用 ObservableObject 吗?

ios - 如何使用数组和内部更多 JSON 来解码 JSON?