簡體   English   中英

如何將 UIImage 從 Swift 上傳到燒瓶服務器?

[英]How to upload UIImage from Swift to a flask server?

有兩部分:

  1. 我想使用Alamofire框架將Swift中的UIImage上傳到服務器。

  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 部分。

我自己找到解決辦法。 只需將 UIImage 轉換為 base64String 並發送到服務器,python 可以解碼 base64String 並將其轉換為 jpg 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM