我正在尝试使用alamofire,multiformdata来发送带有图像和参数的url请求。当我尝试发送请求时,出现类似“线程1:致命错误:在展开可选值“在“ imageobj = qrorimg.image!”行。如果有人帮助我做到这一点,那就太好了。谢谢!
@IBAction func updatedetails(_ sender: Any) {
productidstr = productid.stringValue
imageobj = qrorimg.image!
let acce:String = UserDefaults.standard.string(forKey: "access-tokenn")!
print(acce)
let headers:HTTPHeaders = ["Authorization":"Bearer \(acce)"]
postParameters = ["name":name,"description":descriptionn,"location":address,"email":mail,"phone":phone2,"landline":land2,"validity_start":validstart,"validity_end":validend,"website":web,"offer":offer,"created_at":create,"id":productid]
let ImageData = imageobj.jpegData(compressionQuality: 0.5)
Alamofire.upload(multipartFormData: { (multipartFormData) in
multipartFormData.append(ImageData!, withName: "photo", fileName: self.filename, mimeType: "image/jpeg")
for (key, value) in self.postParameters {
multipartFormData.append(self.name.data(using: String.Encoding.utf8)!,withName: "name")
multipartFormData.append(self.descriptionn.data(using: String.Encoding.utf8)!,withName: "description")
multipartFormData.append(self.address.data(using: String.Encoding.utf8)!,withName: "location")
multipartFormData.append(self.web.data(using: String.Encoding.utf8)!,withName: "website")
multipartFormData.append(self.mail.data(using: String.Encoding.utf8)!,withName: "email")
multipartFormData.append(self.phone2.data(using: String.Encoding.utf8)!,withName: "phone")
multipartFormData.append(self.land2.data(using: String.Encoding.utf8)!,withName: "landline")
multipartFormData.append(self.validstart.data(using: String.Encoding.utf8)!,withName: "validity_start")
multipartFormData.append(self.validend.data(using: String.Encoding.utf8)!,withName: "validity_end")
multipartFormData.append(self.offer.data(using: String.Encoding.utf8)!,withName: "offer")
multipartFormData.append(self.create.data(using: String.Encoding.utf8)!,withName: "created_at")
multipartFormData.append(self.productidstr.data(using: String.Encoding.utf8)!,withName: "id")
}
},to:(Constants.productupdate)+(productidstr),
method: .post,
headers: headers)
{ (result) in
switch result {
case .success(let upload, _, _):
upload.uploadProgress(closure: { (Progress) in
print("Upload Progress: \(Progress.fractionCompleted)")
})
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print(response.request) // original URL request
print(response.response) // URL response
print(response.data) // server data
print(response.result) // result of response serialization
// self.showSuccesAlert()
//self.removeImage("frame", fileExtension: "txt")
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
case .failure(let encodingError):
//self.delegate?.showFailAlert()
print(encodingError)
}
}
}
}
extension ProductdetailupdateViewController:UIImagePickerControllerDelegate{
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info:
[UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
qrorimg.image = image
} else{
print("Something went wrong")
}
dismiss(animated: true, completion: nil)
if let imgUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL{
let imgName = imgUrl.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
let localPath = documentDirectory?.appending(imgName)
var image = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
let data = image.pngData()! as NSData
data.write(toFile: localPath!, atomically: true)
var photoURL = URL.init(fileURLWithPath: localPath!)//NSURL(fileURLWithPath: localPath!)
print(photoURL)
photoURLstr = photoURL.path
print("result \(photoURLstr)")
filename = photoURL.lastPathComponent
fileextension = photoURL.pathExtension // get your file extension
qrorimg.image = image
}
}
}
答案 0 :(得分:0)
您需要先解开图片,然后尝试
if let aImage = qrorimg.image
然后重试。