我想使用 Alamofire 将图片上传到服务器。
我已将所选照片从 imagePicker 更改为 jpegData,并将其用于 MultipartFormData 的追加。
发送通信时,应用程序的内存迅速增加,而使用 debugPrint 时,会提取大量代码。
问题是什么?
我把我的代码放在下面。
let header: HTTPHeaders = [
"Content-Type":"multipart/form-data; boundary=\(boundary)",
"access_token":"\(UserDefaults.standard.string(forKey: "token")!)"
]
AF.upload(multipartFormData: { (MultipartFormData) in
MultipartFormData.append(self.imageData, withName: "img", fileName: "img", mimeType: "img/jpeg")
MultipartFormData.append((self.titleTextField.text?.data(using: .utf8))!, withName: "content")
}, to: "https://httpbin.org/post", method: .post, headers: header).responseJSON { (result) in
debugPrint(result)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let pickedImage = info[.originalImage] as? UIImage else { return }
imageView.image = pickedImage
image = pickedImage
imageData = pickedImage.jpegData(compressionQuality: 0)
imageViewNilLbl.isHidden = true
picker.dismiss(animated: true, completion: nil)
}