无法将图片上传到服务器......
以下是代码:
let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
let data = UIImageJPEGRepresentation(chosenImage, 1)
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(data, withName: "avatar_image")
},
to: url,
method: .put,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})
如果将data
更改为let data = Bundle.main.url(forResource: "flag", withExtension: "png")!
,则会将此图片上传到服务器。
为什么它无法从我的照片库上传图片?
答案 0 :(得分:0)
您可以尝试原创而不是编辑
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
答案 1 :(得分:0)
不知道为什么,但我必须上传文件网址。所以我把文件写到磁盘然后上传......
let fileManager = FileManager.default
let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
let imagePath = documentsPath?.appendingPathComponent("image.jpg")
let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
let data = UIImageJPEGRepresentation(chosenImage, 0.5)
try! UIImageJPEGRepresentation(chosenImage, 0.5)?.write(to: imagePath!)
profileImageURL = imagePath
Alamofire.upload(multipartFormData: { multipartFormData in
multipartFormData.append(profileImageURL, withName: "avatar_image")},
to: "https://api.booze.kz/v1/customers/\(phoneNumber)",
method: .put,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
debugPrint(response)
}
case .failure(let encodingError):
print(encodingError)
}
})