Swift 5 - 使用 Alamofire 和图库中的图像上传 MultipartFormData
Swift 5 - Upload MultipartFormData Using Alamofire with Image from Gallery
我尝试使用多个参数将图库中的图片上传到服务器,但找不到任何有用的示例,因为其中大部分已被弃用。不知道从哪里开始。
func uploadImageAndData(){
let periode = periodeField.text
let kode_wilayah = kodeWilayahField.text
let nama_petugas = namaPetugasField.text
let upload_file = myImageView.image
var parameters = [String:AnyObject]()
parameters = ["periode":periode,
"kode_wilayah":kode_wilayah,
"nama_petugas":nama_petugas,
"uploaded_file": upload_file] as [String : AnyObject]
let URL = "myURL"
Alamofire.upload(
multipartFormData: { (multipartFormData) in
if let imageData = upload_file?.jpegData(compressionQuality: 0.3){
multipartFormData.append(imageData, withName: self.myFileName)
}
}, to:URL)
{ (result) in
switch result {
case .success(let upload, _ , _):
upload.uploadProgress(closure: { (progress) in
//print("uploding")
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
debugPrint(response)
print("done")
print(response.result.value)
}
case .failure(let encodingError):
print("failed")
print(encodingError)
}
}
}
这来自图像选择器功能,从图库中选择图像没有问题。
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
myImageView.image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
let data = image.pngData()! as NSData
data.write(toFile: localPath!, atomically: true)
let photoURL = URL.init(fileURLWithPath: localPath!)
}
self.dismiss(animated: true, completion: nil)
}
我可以获取路径,但无法将其提供给上传功能。非常感谢您的帮助。
试试这个,我想这会对你有所帮助。
func imagupload(){
let periode = periodeField.text
let kode_wilayah = kodeWilayahField.text
let nama_petugas = namaPetugasField.text
let upload_file = myImageView.image
var parameters = [String:AnyObject]()
parameters = ["periode":periode,
"kode_wilayah":kode_wilayah,
"nama_petugas":nama_petugas,
"uploaded_file": upload_file] as [String : AnyObject]
let URL = "myURL"
requestWith(endUrl: URL, imageData: myImageView.image!.jpegData(compressionQuality: 1.0), parameters: parameters, onCompletion: { (json) in
print(json)
}) { (error) in
print(error)
}
}
func requestWith(endUrl: String, imagedata: Data?, parameters: [String : String], onCompletion: ((JSON?) -> Void)? = nil, onError: ((Error?) -> Void)? = nil){
let url = endUrl
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if let data = imagedata{
multipartFormData.append(data, withName: "imagename", fileName: "imagename.jpg", mimeType: "image/jpeg")
}
}, to:url,headers: headers)
{ (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
let json : JSON = JSON(response.result.value)
print(json)
if let err = response.error{
onError?(err)
return
}
onCompletion?(json)
}
case .failure(let error):
//print("Error in upload: \(error.localizedDescription)")
onError?(error)
}
}
}
只有在使用 like 时才会使用 imagupload 功能,
self.imagupload()
我尝试使用多个参数将图库中的图片上传到服务器,但找不到任何有用的示例,因为其中大部分已被弃用。不知道从哪里开始。
func uploadImageAndData(){
let periode = periodeField.text
let kode_wilayah = kodeWilayahField.text
let nama_petugas = namaPetugasField.text
let upload_file = myImageView.image
var parameters = [String:AnyObject]()
parameters = ["periode":periode,
"kode_wilayah":kode_wilayah,
"nama_petugas":nama_petugas,
"uploaded_file": upload_file] as [String : AnyObject]
let URL = "myURL"
Alamofire.upload(
multipartFormData: { (multipartFormData) in
if let imageData = upload_file?.jpegData(compressionQuality: 0.3){
multipartFormData.append(imageData, withName: self.myFileName)
}
}, to:URL)
{ (result) in
switch result {
case .success(let upload, _ , _):
upload.uploadProgress(closure: { (progress) in
//print("uploding")
print("Upload Progress: \(progress.fractionCompleted)")
})
upload.responseJSON { response in
debugPrint(response)
print("done")
print(response.result.value)
}
case .failure(let encodingError):
print("failed")
print(encodingError)
}
}
}
这来自图像选择器功能,从图库中选择图像没有问题。
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
myImageView.image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
let data = image.pngData()! as NSData
data.write(toFile: localPath!, atomically: true)
let photoURL = URL.init(fileURLWithPath: localPath!)
}
self.dismiss(animated: true, completion: nil)
}
我可以获取路径,但无法将其提供给上传功能。非常感谢您的帮助。
试试这个,我想这会对你有所帮助。
func imagupload(){
let periode = periodeField.text
let kode_wilayah = kodeWilayahField.text
let nama_petugas = namaPetugasField.text
let upload_file = myImageView.image
var parameters = [String:AnyObject]()
parameters = ["periode":periode,
"kode_wilayah":kode_wilayah,
"nama_petugas":nama_petugas,
"uploaded_file": upload_file] as [String : AnyObject]
let URL = "myURL"
requestWith(endUrl: URL, imageData: myImageView.image!.jpegData(compressionQuality: 1.0), parameters: parameters, onCompletion: { (json) in
print(json)
}) { (error) in
print(error)
}
}
func requestWith(endUrl: String, imagedata: Data?, parameters: [String : String], onCompletion: ((JSON?) -> Void)? = nil, onError: ((Error?) -> Void)? = nil){
let url = endUrl
let headers: HTTPHeaders = [
"Content-type": "multipart/form-data"
]
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
}
if let data = imagedata{
multipartFormData.append(data, withName: "imagename", fileName: "imagename.jpg", mimeType: "image/jpeg")
}
}, to:url,headers: headers)
{ (result) in
switch result{
case .success(let upload, _, _):
upload.responseJSON { response in
let json : JSON = JSON(response.result.value)
print(json)
if let err = response.error{
onError?(err)
return
}
onCompletion?(json)
}
case .failure(let error):
//print("Error in upload: \(error.localizedDescription)")
onError?(error)
}
}
}
只有在使用 like 时才会使用 imagupload 功能,
self.imagupload()