swift alamofire获取图像文件名和mime类型

时间:2016-08-15 14:16:43

标签: ios swift xcode alamofire

我正在尝试使用Alamofire上传图片,代码如下。 但是此代码要求我知道图像文件名 mimeType

我上传的图片都来自相机和相册。那么在swift中有一种获取图像名称和mime类型的方法吗?

 let image = UIImage(named: "myImage")!

    // define parameters
    let parameters = [
      "id": 23,
      "subject": "images"
    ]

    // Begin upload
    Alamofire.upload(.POST, "upload_url",
      // define your headers here
      headers: ["Authorization": "auth_token"],
      multipartFormData: { multipartFormData in

        // import image to request
        if let imageData = UIImageJPEGRepresentation(image, 1) {
          multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "myImage.png", mimeType: "image/png")
        }

        // import parameters
        for (key, value) in parameters {
          multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
      }, // you can customise Threshold if you wish. This is the alamofire's default value
      encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
      encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
          upload.responseJSON { response in
            debugPrint(response)
          }
        case .Failure(let encodingError):
          print(encodingError)
        }
    })

0 个答案:

没有答案