gpt4 book ai didi

ios - 尝试通过 Alamofire 4.0 上传图像时,类型 "ParameterEncoding"没有成员 'URL'

转载 作者:行者123 更新时间:2023-11-30 12:55:12 26 4
gpt4 key购买 nike

在将 Alamofire 更新到 4.0 版本之前,我使用相同的代码将图像上传到服务器,它运行得很好,但现在我遇到了这些错误,我尝试了很多,但每次都会出现新错误:

第一个错误: enter image description here

第二个错误: enter image description here

我的代码:

func uploadFile()  {

let image = UIImage(named: "ios9.jpg")
let newimage : NSData = UIImageJPEGRepresentation(selectedImage.image!, 32)! as NSData

let newRandomName = randomStringWithLength(32)
let fname = (newRandomName as String) + ".jpg"

let parameters = [
"pic" :NetData(data: newimage as Data, mimeType: .ImageJpeg, filename: fname),
"msg" :self.detailmsg,
"customer_key" : self.CUSTOMER_KEY
] as [String : Any]



let urlRequest = self.urlRequestWithComponents("link", parameters: parameters as NSDictionary)


//First error here
Alamofire.upload(urlRequest.0, data: urlRequest.1)
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
print("\(totalBytesWritten) / \(totalBytesExpectedToWrite)")
}
.responseJSON { response in
debugPrint(response)
}

}


func urlRequestWithComponents(_ urlString:String, parameters:NSDictionary) -> (URLRequestConvertible, NSData) {

// create url request to send
let mutableURLRequest = NSMutableURLRequest(url: URL(string: urlString)!)
mutableURLRequest.httpMethod = Alamofire.HTTPMethod.post.rawValue
//let boundaryConstant = "myRandomBoundary12345"
let boundaryConstant = "NET-POST-boundary-\(arc4random())-\(arc4random())"
let contentType = "multipart/form-data;boundary="+boundaryConstant
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")


// create upload data to send
let uploadData = NSMutableData()

// add parameters
for (key, value) in parameters {

uploadData.append("\r\n--\(boundaryConstant)\r\n".data(using: String.Encoding.utf8)!)

if value is NetData {
// add image
let postData = value as! NetData


//uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(postData.filename)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

// append content disposition
let filenameClause = " filename=\"\(postData.filename)\""
let contentDispositionString = "Content-Disposition: form-data; name=\"\(key)\";\(filenameClause)\r\n"
let contentDispositionData = contentDispositionString.data(using: String.Encoding.utf8)
uploadData.append(contentDispositionData!)


// append content type
//uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!) // mark this.
let contentTypeString = "Content-Type: \(postData.mimeType.getString())\r\n\r\n"
let contentTypeData = contentTypeString.data(using: String.Encoding.utf8)
uploadData.append(contentTypeData!)
uploadData.append(postData.data as Data)

}else{
uploadData.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".data(using: String.Encoding.utf8)!)
}
}
uploadData.append("\r\n--\(boundaryConstant)--\r\n".data(using: String.Encoding.utf8)!)



// return URLRequestConvertible and NSData
// return (Alamofire.ParameterEncoding.encode(mutableURLRequest as! ParameterEncoding).0, uploadData)

//Second error here
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
}

最佳答案

let image = UIImage(named: "ios9.jpg")
let params:[String:Any] = ["msg": self.detailmsg,
"customer_key": self.CUSTOMER_KEY]

let urlRequest = try! URLRequest(url: url, method: .post, headers: nil)

Alamofire.upload(multipartFormData: { (multipartFormData) in

if let imageData = UIImageJPEGRepresentation(image!, 1.0){
multipartFormData.append(imageData, withName: "pic", fileName:"image.jpg", mimeType: "image/jpg")
}
for (key, value) in self.parameters! {
let data = "\(value)".data(using: .utf8)
multipartFormData.append(data!, withName: key)
}

}, with: urlRequest) { (result) in
switch result{
case .success(let request, _, _):
request.responseJSON { response in
debugPrint(response)
}
case .failure(let error):
print(error.localizedDescription)
}
}

关于ios - 尝试通过 Alamofire 4.0 上传图像时,类型 "ParameterEncoding"没有成员 'URL',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40503040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com