【问题标题】:uploading photo to web service with Swift使用 Swift 将照片上传到网络服务
【发布时间】:2016-07-22 11:53:45
【问题描述】:

我想上传一张照片。我检查了 Alamofire,但所有示例都使用 Json 和编码。我需要上传用应用程序拍摄的照片并将其作为文件发送到网络服务。 Web 服务的响应也是字符串。我怎样才能做到?

【问题讨论】:

    标签: ios swift web-services post alamofire


    【解决方案1】:
    Alamofire.upload(.POST, urlString, multipartFormData: {
            multipartFormData in
            if let _image = self.profilePic.image {
                if let imageData = UIImagePNGRepresentation(_image) {
                    multipartFormData.appendBodyPart(data: imageData, name: "user_image", fileName: "file.png", mimeType: "image/png")
                }
            }
            for (key, value) in userInfo {
                multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
            }
            }, encodingCompletion: { encodingResult in
                switch encodingResult {
                case .Success(let upload, _, _):
                    upload.responseJSON { response in
                        debugPrint(response)
                    }
                case .Failure(let encodingError):
                    print(encodingError)
                }
            }
        )
    

    self.profilePic.image 是您的图像。

    【讨论】:

      猜你喜欢
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 1970-01-01
      相关资源
      最近更新 更多