本文介绍了使用Alamofire上传具有base64String编码的多个图像的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力使用Alamofire将多个图像发送到后端。我在NSMutableArray中添加了base64图像字符串。现在, 我正在尝试将此字符串数组发送到服务器 ,但是它不起作用。
I am working out to send multiple images to backend using Alamofire. I have got base64 strings of images added in a NSMutableArray. Now I am trying to send this array of strings to server but its not working.
我尝试过通过这种方式上传单个图片,它的工作原理非常酷,但是为什么不包含一系列图片 。
I have tried uploading a single image by this way and it works super cool but why not an array of images.
图像拾取-
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
slctdImage = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
uploadImage(slctdImage)
}
正在上载
func uploadImage( image:UIImage)
{
let pic :NSData = UIImageJPEGRepresentation(image, 0.5)!
let str = pic.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
//imageArray is declared as var imageArray = NSMutableArray()
imageArray.addObject(str)
//when imageArray count reaches 3 I send the images
if imageArray.count==3
{
let parameters = [
"task": "doNotification",
"image" : imageArray,
"select_category" : "exams",
"select_type" : "quarterly",
"class" : "1",
"repliable" : "0",
"select_students" : ["25","26"],
"select_group" : "Super Users",
"title" : "Hello",
"text" : "asdfsdf",
"date" : "2015-12-15",
"time" : "10:50 AM"]
Alamofire.request(.POST, UrlClass.baseUrl, parameters:parameters ) .response {
(request, response, data, error) in
// self.startParsing(data!)
print(response)
}
}
}
那么如何解决这个问题问题?请提出对代码的修改建议或其他方式。
So how to solve this problem? Please suggest modifications to the code or else some other means.
推荐答案
使用以下代码将图像上传到服务器
use Following code to Upload image to the server
let image = UIImage(named:"testImage")
Alamofire.upload(
.POST,
URL,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: UIImageJPEGRepresentation(image, 1)!, name: "imageFile",
fileName: "image.jpg", mimeType: "image/jpeg")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
dispatch_async(dispatch_get_main_queue()) {
let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
//progress(percent: percent)
print(percent)
}
}
upload.validate()
upload.responseJSON { response in
if response.result.error != nil {
// failure
} else {
// success
}
}
case .Failure(let encodingError):
print(encodingError)
//failure
}
}
)
}
对于多张图像
Alamofire.upload(
.POST,
urlString,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: "122222".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"Id")
multipartFormData.appendBodyPart(data: "test1111".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"sId")
multipartFormData.appendBodyPart(data: "", name :"ContractorIds")
multipartFormData.appendBodyPart(data:date.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"ExpectedOn")
multipartFormData.appendBodyPart(data:date.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"ExpiresOn")
multipartFormData.appendBodyPart(data: data.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"Notes")
for image in images {
if let imageData = UIImageJPEGRepresentation(image, 0.05) {
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.jpeg", mimeType: "image/jpeg")
}
}
},
这篇关于使用Alamofire上传具有base64String编码的多个图像的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!