如何将UIImage从Swift上传到烧瓶服务器?

问题描述 投票:0回答:1

有两个部分:

  1. 我想使用Alamofire框架将UIImage中的Swift上传到服务器。
  2. 运行flask-RESTful的服务器,用于接收该映像并将其存储在服务器上。

这是我的Swift代码:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        // your chosen image
        let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage

        // save to local documents
        let fileManager = FileManager.default
        let rootPath = NSSearchPathForDirectoriesInDomains(.documentDirectory,
                                                           .userDomainMask, true)[0] as String
        let filePath = "\(rootPath)/pickedimage.jpg"
        let imageData = pickedImage.jpegData(compressionQuality: 1.0)
        fileManager.createFile(atPath: filePath, contents: imageData, attributes: nil)

        // upload
        if (fileManager.fileExists(atPath: filePath)){

            let imageURL = URL(fileURLWithPath: filePath)

            Alamofire.upload(imageURL, to: "http://xxxx")
                .responseString { response in
                    print("Success: \(response.result.isSuccess)")
                    print("Response String: \(response.result.value ?? "")")
            }
        }
    }

我想知道Swift代码是否正确以及如何处理flask-RESTful部分。

ios swift flask alamofire
1个回答
0
投票

我自己找到了解决方案。只需将UIImage转换为base64String并发布到服务器,python就可以解码base64String并将其转换为jpg文件。

© www.soinside.com 2019 - 2024. All rights reserved.