我正在尝试将从UIImagePickerController中选择的图像上传到我在AWS上托管的Web服务器。我正在关注本教程:https://www.udemy.com/swift-php-mysql-user-sign-up-sign-in-sign-out-and-more/learn/v4/overview,我收到了一堆Argument labels '(_:)' do not match any available overloads
错误。我试过搜索这个错误,但我找不到任何与我当前代码有关的内容。我没有在教程中使用原始代码,我正在尝试将其转换为Swift3并使用以下答案:iOS swift NSMutableData has no member appendString来遵循最佳编码实践。这是我目前的代码:
func createBodyWithParameters(_ parameters: [String: String]?, filePathKey: String?, imageDataKey: Data, boundary: String) -> Data {
var body = Data();
if parameters != nil {
for (key, value) in parameters! {
body.append(Data("--\(boundary)\r\n"))
body.append(Data("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n"))
body.append(Data("\(value)\r\n"))
}
}
let filename = "user-profile.jpg"
let mimetype = "image/jpg"
body.append(Data("--\(boundary)\r\n"))
body.append(Data("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n"))
body.append(Data("Content-Type: \(mimetype)\r\n\r\n"))
body.append(imageDataKey)
body.append(Data("\r\n"))
body.append(Data("--\(boundary)--\r\n"))
return body as Data
}
有人可以解释我做错了什么吗?谢谢!
答案 0 :(得分:0)
您不能像这样初始化数据:
Data("foo")
你能做的是:
Data("foo".utf8)
就像您提供的链接一样。 (你错过了.utf8)
编辑:是的,那个人说他不想使用那个代码,但是听我说,这个版本的扩展不允许有损连接,只要你在字符串里面写任何东西,数据永远不会是零。extension NSMutableData {
func appendString(_ string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: false)
append(data!)
}
}
用法:
body.appendString("foo")
像这样初始化身体:
var body = NSMutableData()
像这样回归
return body as Data
答案 1 :(得分:0)
试试这段代码。您可以使用此代码将图像上传到swift3中的网络服务器
func generateBoundaryString() -> String
{
return "Boundary-\(NSUUID().uuidString)"
}
func photoDataToFormData(parameters: [String: String]?, data: Data, boundary:String, fileName:String) -> Data
{
var fullData = Data()
if parameters != nil {
for (key, value) in parameters! {
print ("Key: \(key), Value: \(value)")
let str = "--\(boundary)\r\n" + "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n" + "\(value)\r\n"
let data = str.data(using: String.Encoding.utf8, allowLossyConversion: true)
fullData.append(data!)
}
}
// 1 - Boundary should start with --
let lineOne = "--" + boundary + "\r\n"
fullData.append(lineOne.data(using: String.Encoding.utf8, allowLossyConversion: false)!)
// 2
let lineTwo = "Content-Disposition: form-data; name=\"image\"; filename=\"" + fileName + "\"\r\n"
NSLog(lineTwo)
fullData.append(lineTwo.data(using: String.Encoding.utf8, allowLossyConversion: false)!)
// 3
let lineThree = "Content-Type: image/jpg\r\n\r\n"
fullData.append(lineThree.data(using: String.Encoding.utf8, allowLossyConversion: false)!)
// 4
fullData.append(data)
// 5
let lineFive = "\r\n"
fullData.append(lineFive.data(using: String.Encoding.utf8, allowLossyConversion: false)!)
// 6 - The end. Notice -- at the start and at the end
let lineSix = "--" + boundary + "--\r\n"
fullData.append(lineSix.data(using: String.Encoding.utf8, allowLossyConversion: false)!)
return fullData
}
在上面的代码之后,你需要在Api函数中的任务之前调用一个函数。我在下面提到了这段代码。
let imageData = UIImageJPEGRepresentation(profileImage.image!, 0.5)
let boundary = generateBoundaryString()
let fullData = photoDataToFormData(parameters: parameters as? [String :
String], data: imageData!, boundary: boundary, fileName: "Test.jpg")
request.setValue("multipart/form-data; boundary=" + boundary,
forHTTPHeaderField: "Content-Type")
// REQUIRED!
request.setValue(String(fullData.count), forHTTPHeaderField: "Content-
Length")
request.httpBody = fullData
答案 2 :(得分:0)
我通过在类ViewController之后添加以下行来解决swift 4中的这个问题:
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}
extension NSMutableData {
func appendString(string: String) {
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
append(data!)
}
}
我的意思是类Viewcontroller在将ViewController添加到项目时创建的类。 示例下一课:
class ViewController2: UIViewController ,UINavigationControllerDelegate , UIImagePickerControllerDelegate{
// my code
}