[英]How to upload UIImage from Swift to a flask server?
有兩部分:
我想使用Alamofire
框架將Swift
中的UIImage
上傳到服務器。
運行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.