我具有使用Alamofire上传图像的功能。 当我上传它时,图像已损坏。但是,当我将其保存到设备时,可以打开图像。
这是上传代码
Alamofire.upload(multipartFormData: { (_multipartFormData:MultipartFormData) in
for (key, value) in parameters! {
if key == "foto_id" {
_multipartFormData.append(
"\(value)".data(using: String.Encoding.utf8)!,
withName: key,
fileName: "_foto_id.png",
mimeType: "image/png"
)
} else if key == "foto_selfie" {
_multipartFormData.append(
"\(value)".data(using: String.Encoding.utf8)!,
withName: key,
fileName: "_foto_selfie.png",
mimeType: "image/png"
)
} else {
//Data other than image
_multipartFormData.append((value as! String).data(using: .utf8)!, withName: key)
}
}
}, usingThreshold: 1, to: BASE_URL, method: .post) { (encodingResult:SessionManager.MultipartFormDataEncodingResult) in
switch encodingResult {
case .success(let upload, _, _):
upload.responseObject{(response: DataResponse<clsResponsePostFotoID>) in
let reqLoginResponse = response.result.value
completionHandler(reqLoginResponse, nil)
}
break
case .failure(let encodingError):
print(encodingError)
// completionHandler(nil,encodingError as NSError?)
break
}
}
}
如果我将"\(value)".data(using: String.Encoding.utf8)!
更改为value as! Data
,它将显示错误
Cannot invoke 'append' with an argument list of type '(Data?, withName: String, fileName: String, mimeType: String)'
Overloads for 'append' exist with these partially matching parameter lists: (Data, withName: String, fileName: String, mimeType: String), (URL, withName: String, fileName: String, mimeType: String)
我该如何解决? 谢谢
答案 0 :(得分:0)
尝试使用此代码:
if let image = value as? UIImage, let imageData = UIImageJPEGRepresentation(image, 0.6) {
multipartFormData.append(imageData, withName: key, fileName: key+".jpg", mimeType: "image/jpeg")
}
答案 1 :(得分:0)
您的代码有点混乱,请先使其清晰并格式化。顺便说一句,尝试我的代码:
var imageFrontData: Data?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
picker.dismiss(animated: true, completion: nil)
var uploadImage = WMCameraUtils.fixOrientation((info[UIImagePickerControllerEditedImage] as? UIImage)!)
imageFrontData = UIImageJPEGRepresentation(uploadImage, 1.0)!
self.uploadFile()
}
static func fixOrientation(_ img:UIImage) -> UIImage {
if (img.imageOrientation == UIImageOrientation.up) {
return img;
}
UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale);
let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
img.draw(in: rect)
let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
return normalizedImage;
}
func uploadFile() {
Alamofire.upload(
multipartFormData: { multipartFormData in
/* self.imageFrontData is image data object */
if let data = self.imageFrontData {
multipartFormData.append(data, withName: "front-photo", fileName: "front-photo.png", mimeType: "image/png")
}
for (key, value) in parameters {
multipartFormData.append((value?.data(using: String.Encoding.utf8)!)!, withName: key)
}
}, usingThreshold: UInt64.init(), to: uploadURL!, method: .post, headers: headers) { (result) in
switch result {
case .success(let upload, _, _):
upload.responseJSON { response in
if let err = response.error {
}
/* do rest */
}
case .failure(let encodingError):
/* error */
}
}
}
答案 2 :(得分:0)
迟来的答案,但可能会帮助别人。
以多部分上传图像时,如果您有字符串字节,请使用base64而不是utf8对其进行编码。
extension String {
var base64Data: Data? {
return Data.init(base64Encoded: self, options: .init(rawValue: 0))
}
}
例如:
"\(bytes)".base64Data