有两个部分:
Alamofire
框架将UIImage
中的Swift
上传到服务器。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文件。