我想将图像发送到php api作为链接或任何方法,以便php代码将其保存到文件夹目录中。 这是我的图片选择代码。
@objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickerImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let extURL = info[UIImagePickerControllerImageURL] as! NSURL
print("image url \(extURL)")
let ext = extURL.lastPathComponent
imageURL = ext!
self.imgRef.contentMode = .scaleAspectFit
self.imgRef.image = pickerImage
}
picker.dismiss(animated: false, completion: nil)
}
如何通过php api将图像发送到此处以存储到服务器文件夹
答案 0 :(得分:0)
使用此Alamofire方法:
func hitMultipartForImage(_ params:Dictionary<String,String>,image:UIImage?,imageParamName : String?,unReachable:(() -> Void),handler:@escaping ((Dictionary<String,Any>?) -> Void)) {
print_debug("Params: \(params)")
let BASE_URL = "http:" //Your end point
var imageParam = "image"
if imageParamName ?? "" != ""{
imageParam = imageParamName!
}
Alamofire.upload(
multipartFormData: { multipartFormData in
for (key, value) in params {
if "appendURL" != key {
multipartFormData.append(value.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: key)
}
}
if image != nil{
let imgData = UIImageJPEGRepresentation(image!, 0.5)
if imgData != nil {
print_debug("img data available")
multipartFormData.append(imgData!, withName: imageParam, fileName: "file.png", mimeType: "image/png")
}
}
},
to: BASE_URL,
encodingCompletion: { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.responseJSON { response in
switch response.result {
case .success:
if let jsonDict = response.result.value as? Dictionary<String,Any> {
print_debug("Json Response: \(jsonDict)") // serialized json response
handler(jsonDict)
}
else{
handler(nil)
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Server Response: \(utf8Text)") // original server data as UTF8 string
}
break
case .failure(let error):
handler(nil)
print_debug(error)
break
}
}
case .failure(let encodingError):
print(encodingError)
}
}
)
}