现在,我已经通过下面给出的代码将一个图像上传到服务器端脚本的服务器上。现在我有一个UIImage
数组,我想知道如何使用UIImageJPEGRepresentation(myImageView.image!, 0.1)
在UIImageView
数组中发布所有图像?
func uploadImage()
{
let postPictureUrl = NSURL(string: "http://www.23look.com/merchant/verify")
let request = NSMutableURLRequest(URL: postPictureUrl!)
request.HTTPMethod="POST"
let param=[
"mer_name" : shopNameUITF.text!,
"mer_tel" : shopTelephoneUITF.text!,
"mer_address" : shopAddressUITF.text!,
"lat" : "39.6892",
"lng" : "115.9239",
"token": KeychainWrapper.stringForKey("tokenValue")!,
"mer_type": "smalll"
]
let abc = KeychainWrapper.stringForKey("tokenValue")!
let boundary = generateBoundaryString()
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let imageData = UIImageJPEGRepresentation(myImageView.image!, 0.1)
if imageData==nil { print("image data is nil"); return }
request.HTTPBody = createBodyWithParameters(param, filePathKey: "mer_license", imageDataKey: imageData!, boundary: boundary)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}
//You can print out response object
print("***** response = \(response)")
// Print out reponse body
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("**** response data = \(responseString!)")
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
dispatch_async(dispatch_get_main_queue(), {
print(NSString(data: data!, encoding:NSUTF8StringEncoding)!)
print(json)
})
} catch let err {
print(err)
}
}
task.resume()
}
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}
func createBodyWithParameters(parameters:[String:String]?, filePathKey: String?, imageDataKey:NSData, boundary: String) -> NSData {
var body=NSMutableData()
if parameters != nil {
for(key, value) in parameters! {
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
body.appendString("\(value)\r\n")
}
}
let filename = "user-profile.jpg"
let mimetype = "image/jpg"
body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
body.appendString("Content Type: \(mimetype)\r\n\r\n")
body.appendData(imageDataKey)
body.appendString("\r\n")
body.appendString("-\(boundary)-\r\n")
return body
}
需要以下参数:
mer_name - String
mer_tel - String
mer_address - string
lng - string
lat - string
mer_licence - 文件类型(这是我的图片将被上传的地方)
token - string
mer_type - string
答案 0 :(得分:18)
您应该在createBodyWithParameters中进行更改,如下所示.........
当你有多张图片时......
return this.waitFor({
pollingInterval : 100,
viewName : "sap.ui.commons.MessageBox",
check : function () {
return !!sap.ui.test.Opa5.getJQuery()(".MessageBox").length;
},
success : function () {
ok(true, "I see a confirm popup");
},
errorMessage : "Did not find confirmation popup"
});
答案 1 :(得分:0)
doOnMain {
Loading.sharedInstance.startloading()
}
let url = NSURL(string:"URL")
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let headers = ["Accept-Language": "en"]
request.allHTTPHeaderFields = headers
let boundary = generateBoundaryString()
//define the multipart request type
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
if (imgRefund.image == nil){
return
}
// First Image
let image_data = imgRefund.image!.pngData()
if(image_data == nil){
return
}
// Second Image
let image_data1 = imgRefund1.image!.pngData()
if(image_data1 == nil){
return
}
let body = NSMutableData()
let fname = "filename"
let fname1 = "filename1"
let mimetype = "image/png"
let parameters = ["key":"value","key":"value"]
if parameters != nil {
for (key, value) in parameters {
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(value)\r\n".data(using: String.Encoding.utf8)!)
}
}
// First Image
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"test\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("hi\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"parameter\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!) // paramenter name
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
// Second Image
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"test\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("hi\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=\"parameter1\"; filename=\"\(fname1)\"\r\n".data(using: String.Encoding.utf8)!) // paramenter name 1
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(image_data1!)
body.append("\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body as Data
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest) {
(
data, response, error) in
guard let _:NSData = data! as NSData, let _:URLResponse = response, error == nil else {
print("error")
return
}
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("****** response data = \(responseString!)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
print("****** response json = \(json!)")
if(json?.value(forKey: "status") as! Int == 200){
print(json)
}
else{
}
}catch{
print(error)
}
}
task.resume()
func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().uuidString)"
}