iOS: Alamofire 多部分图像提交

iOS: Alamofire multipart image submit

我正在尝试将具有其他一些值的图像提交给 "Article" 对象,这有效,除非我在添加图像时出现以下错误:

ArgumentError (invalid byte sequence in UTF-8)

这是我提交新对象的函数:

func submitArticle() {
    let description = fullDescription.text!
    let categoriesId = categoriesArray[self.categoryPicked]["id"] as! Int
    let locationId = locationsArray[self.locationPicked]["id"] as! Int
    let userId = User.sharedInstance.userId

    Alamofire.upload(.POST, submitLink, multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(data: self.name.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[name]")
        multipartFormData.appendBodyPart(data: description.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[description]")
        multipartFormData.appendBodyPart(data: self.sDescription.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[subdescription]")
        multipartFormData.appendBodyPart(data: "\(categoriesId)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[category_id]")
        multipartFormData.appendBodyPart(data: "\(locationId)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[location_id]")
        multipartFormData.appendBodyPart(data: "\(self.lng)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[Xcoordinate]")
        multipartFormData.appendBodyPart(data: "\(self.lat)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[Ycoordinate]")
        multipartFormData.appendBodyPart(data: "\(userId)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[user_id]")
        for var i = 0; i < self.photoArray.count; i++ {
            let imageData = UIImageJPEGRepresentation(self.photoArray[i], 1)
            multipartFormData.appendBodyPart(data: "image_iOS_device".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "article[article_images_attributes][][title]")
            multipartFormData.appendBodyPart(data: imageData!, name: "article[article_images_attributes][][image]", mimeType: "image/jpeg")
        }
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })
}

所以我的问题是如何正确地将图像添加到这个多部分表单中?

我修好了:

func submitArticle() {
    let description = fullDescription.text!
    let categoriesId = categoriesArray[self.categoryPicked]["id"] as! Int
    let locationId = locationsArray[self.locationPicked]["id"] as! Int
    let userId = User.sharedInstance.userId

    Alamofire.upload(.POST, submitLink, multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(data: self.name.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[name]")
        multipartFormData.appendBodyPart(data: description.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[description]")
        multipartFormData.appendBodyPart(data: self.sDescription.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[subdescription]")
        multipartFormData.appendBodyPart(data: "\(categoriesId)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[category_id]")
        multipartFormData.appendBodyPart(data: "\(locationId)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[location_id]")
        multipartFormData.appendBodyPart(data: "\(self.lng)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[Xcoordinate]")
        multipartFormData.appendBodyPart(data: "\(self.lat)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[Ycoordinate]")
        multipartFormData.appendBodyPart(data: "\(userId)".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name :"article[user_id]")
        var index = 0
        for photo in self.photoArray {
            var (imagePicked, imageExtension) = photo
            var imagePickedData = NSData()

            switch imageExtension {
                case "png": imagePickedData = UIImagePNGRepresentation(imagePicked)!
                case "jpg": imagePickedData = UIImageJPEGRepresentation(imagePicked, 1)!
                default: imagePickedData = UIImageJPEGRepresentation(imagePicked, 1)!
            }
            imagePickedData = UIImageJPEGRepresentation(imagePicked, 1)!
            multipartFormData.appendBodyPart(data: "image_iOS_device".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "article[article_images_attributes[\(index)][title]]")
            multipartFormData.appendBodyPart(data: imagePickedData, name: "article[article_images_attributes[\(index)][image]]", fileName: "image_\(index).\(imageExtension)", mimeType: "image/\(imageExtension)")
            index++
        }
        index = 0
        for video in self.videoArray {
            multipartFormData.appendBodyPart(data: "video_iOS_device".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "article[article_videos_attributes[\(index)][title]]")
            multipartFormData.appendBodyPart(fileURL: video, name: "article[article_videos_attributes[\(index)][video]]", fileName: "video_\(index).mp4", mimeType: "video/mp4")
            index++
        }
        index = 0
        for audio in self.audioArray {
            multipartFormData.appendBodyPart(data: "audio_iOS_device".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!, name: "article[article_audios_attributes[\(index)][title]]")
            multipartFormData.appendBodyPart(fileURL: audio, name: "article[article_audios_attributes[\(index)][audio]]", fileName: "audio_\(index).mp3", mimeType: "audio/mpeg")
            index++
        }
    },
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })

}