Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/95.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否可以在Alamofire中发送带有图像的文本数据?_Ios_Swift_Alamofire - Fatal编程技术网

Ios 是否可以在Alamofire中发送带有图像的文本数据?

Ios 是否可以在Alamofire中发送带有图像的文本数据?,ios,swift,alamofire,Ios,Swift,Alamofire,我需要通过Alamofire发送一些文本数据和图像(多部分),例如: { "name": "Me", "age": "23", "image": file_goes_here } 我知道如何将文本数据和图像分别作为多部分发送,但我不知道如何在一个相同的请求中发送所有文本数据和图像。有可能吗?你可以用这样的东西- Alamofire.upload(.POST, urlString, headers: nil, multipartFormData: { (multipar

我需要通过Alamofire发送一些文本数据和图像(多部分),例如:

{
    "name": "Me",
    "age":  "23",
    "image": file_goes_here
}

我知道如何将文本数据和图像分别作为多部分发送,但我不知道如何在一个相同的请求中发送所有文本数据和图像。有可能吗?

你可以用这样的东西-

Alamofire.upload(.POST, urlString, headers: nil, multipartFormData: { (multipartFormData) -> Void in

    multipartFormData.appendBodyPart(data: name.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"name")
    multipartFormData.appendBodyPart(data: age.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"age")
    //.....

    }, encodingCompletion: { encodingResult in
        //...

        }
    }
)

您可以将图像数据编码为base64格式

// Use image name from bundle to create NSData
let image : UIImage = UIImage(named:"imageName")!
let imageData = UIImagePNGRepresentation(image)
Swift 2.0编码

let base64String = imageData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) 
Swift 2.0解码

let base64String = imageData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters) 
然后将base64字符串附加到post数据中

{
    "name": "Me",
    "age":  "23",
    "image": base64String
}

谢谢,但我需要把它作为multipart@JohnDoe对不起,我不明白你说的多部分是什么意思?