0
我使用alamofire框架將圖像/視頻上傳到服務器,當我試圖通過iphone 6s上傳拍攝的圖像時,圖像無法上傳,但是當我嘗試上傳小圖片效果非常好,alamofire支持的最大尺寸是多少,如果我們沒有最大尺寸,我可以如何解決這個問題?The Alamofire is not for a larg image
func uploadFile() {
//let image = UIImage(named: "ios9.jpg")
let newimage : NSData = UIImageJPEGRepresentation(image.image!, 32)!
let newRandomName = randomStringWithLength(32)
let fname = (newRandomName as String) + ".jpg"
let parameters = [
"pic" :NetData(data: newimage, mimeType: .ImageJpeg, filename: fname),
"msg" :self.msg,
"customer_key" : self.CUSTOMER_KEY
]
let urlRequest = self.urlRequestWithComponents("myURLhere", parameters: parameters)
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: NSURL(string: urlString)!)
mutableURLRequest.HTTPMethod = Alamofire.Method.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.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
if value is NetData {
// add image
let postData = value as! NetData
// append content disposition
let 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)!) // mark this.
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 (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
}
func randomStringWithLength (len : Int) -> NSString {
let letters : NSString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var randomString : NSMutableString = NSMutableString(capacity: len)
for (var i=0; i < len; i++){
var length = UInt32 (letters.length)
var rand = arc4random_uniform(length)
randomString.appendFormat("%C", letters.characterAtIndex(Int(rand)))
}
return randomString
}
你能解釋一下你的答案,什麼是差異會後聲明,什麼是最大尺寸可我上傳它通過框架正如我上面問! –