進出口使用下面的代碼到一個單一的圖像上傳到服務器(帶參數),但是我一直在試圖上傳多張圖片,但不能讓它的工作多個圖像文件
創建NSData對象(保存在臨時目錄中的圖像)。使用netdata類here
func uploadData(){
//create image data objects
let filemanager:NSFileManager = NSFileManager()
let files = filemanager.enumeratorAtPath(tempPicPath)
var counter:Int = Int()
while let file: AnyObject = files?.nextObject() {
imagePathCollection.append(tempPicPath + "/" + (file as NSString))
}
//convert to data objects()
for path in imagePathCollection{
var image: UIImage? = UIImage(contentsOfFile: path)
if image != nil {
var sizeOfImage = image?.size
var resizedImage = RBSquareImageTo(image!, sizeOfImage!)
var imageData = UIImageJPEGRepresentation(resizedImage, 75)
imageDataCollection.append(NetData(data: imageData!, mimeType:MimeType(rawValue: "image/jpeg")!, filename: "myImage.jpeg"))
}
}
if(imageDataCollection.count > 1){
for (index, element) in enumerate(imageDataCollection) {
multipleImageUpload(imageDataCollection[index])
}
}else{
singleImageUpload(imageDataCollection[0])
}
}
使用Alamofire上傳
func extrasImageUpoload(urlRequest:(URLRequestConvertible, NSData))->Request{
let request = Alamofire.upload(urlRequest.0, urlRequest.1)
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println("progress : \(totalBytesWritten)/\(totalBytesExpectedToWrite)")
}
return request
}
生成Alamofire請求
func urlRequestWithComponents(parameters:NSDictionary, uploadType:String) -> (URLRequestConvertible, NSData) {
var mutableURLRequest:NSMutableURLRequest = NSMutableURLRequest()
//set url type
if(uploadType == "extra"){
mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: baseURL + "ticket/extra")!)
}else if(uploadType == "checkList"){
//uploadUrl = "ticket/mark"
mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: baseURL + "ticket/mark")!)
}
// create url request to send
//var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: baseURL + "ticket/extra")!)
mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
//let boundaryConstant = "myRandomBoundary12345"
let boundaryConstant = "NET-POST-boundary-\(arc4random())-\(arc4random())"
let contentType = "multipart/form-data;boundary="+boundaryConstant
//let pgToken = "c9049df83e8bfd7a3dfaef279cdb74478330ff2a"
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
mutableURLRequest.setValue(self.token, forHTTPHeaderField: "PG-Auth-Token")
// create upload data to send
let uploadData = NSMutableData()
// add parameters
for (key, value) in parameters {
uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
if value is NetData {
// add image
var postData = value as NetData
//uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(postData.filename)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!
var filenameClause = " filename=\"\(postData.filename)\""
let contentDispositionString = "Content-Disposition: form-data; name=\"\(key)\";\(filenameClause)\r\n"
let contentDispositionData = contentDispositionString.dataUsingEncoding(NSUTF8StringEncoding)
uploadData.appendData(contentDispositionData!)
// append content type
//uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
let contentTypeString = "Content-Type: \(postData.mimeType.getString())\r\n\r\n"
let contentTypeData = contentTypeString.dataUsingEncoding(NSUTF8StringEncoding)
uploadData.appendData(contentTypeData!)
uploadData.appendData(postData.data)
}else{
uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
}
}
uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
// return URLRequestConvertible and NSData
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
}
與NSData的集合沿所述參數陣列,如下
var params = ["pic":imageData,"ticketid":ticketID, "appversion":Utilities().getAPPVersion(), "description":"bla bla bla", "cost":50]
「imageData」是圖像的NSData對象的集合。代碼工作正常,但收到的圖像的NSData數組作爲一個空的數組。然而,其他參數包括「appversion」和「description」將會收到罰款
感謝我給它去 – Yrol 2015-02-12 06:56:03
我得到這個當我嘗試做多載「對未渲染的視圖進行快照會產生空白的快照,確保在屏幕更新後快照或快照之前,您的視圖至少已呈現一次。」 「.NetData」沒有實現methodSignatureForSelector: - 提前出現問題「 – Yrol 2015-02-12 10:42:52
你的快照錯誤是一個非常不同的問題,與我提出的修改無關。我會建議提交一個不同的問題,並查看[drawViewHierarchyInRect:](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/occ/instm/UIView/drawViewHierarchyInRect :afterScreenUpdates :)方法。至於'.NetData'問題,你還沒有發佈足夠的信息去繼續。再次,我不認爲這是我發佈的代碼的問題。 – cnoon 2015-02-12 16:15:47