Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST image with swift client from generated swagger code

Tags:

ios

swift

swagger

I want to post an image using the generated swift-client. After a lot of researching I think the best way to specify this is:

/user/profilepicture:
    put:
      description: |
        upload profile picture of user
      consumes:
        - multipart/form-data
      parameters:
        - name: profilePhoto
          in: formData
          type: file

The generated swift client function signature is:

public class func usersProfilepicturePut(profilePhoto profilePhoto: NSURL? = nil, completion: ((error: ErrorType?) -> Void))

The problem I am having is the NSURL type. The reason is that it seems very difficult to get an NSURL out of a UIImage, especially if the photo has been taken from the camera with the UIImagePickerController.

Then again I do not want to change the type of the parameter to a string, and use a base64 encoding because it adds a lot of overhead to convert the image to a string.

  • Could someone verify that my yaml spec is correct? (I am choosing file type, because the only other data type I could use to upload a photo is string, with format Byte, but that would lead in an overhead to convert the photo in string.
  • If it is indeed correct, does anyone know if there is a way to get an NSURL from a UIImage. This second question exists, however the answer in [Getting the URL of picture taken by camera with Photos Framework does not return a URL but a string identifier. Also other answers to similar questions all suggest to save the image and then retrieve it again just to get an NSURL which seems hacky. So should I change the generated implementation to accept an NSData type, or do you have anything better to suggest?
like image 681
Andreas Papageorgiou Avatar asked Oct 04 '16 10:10

Andreas Papageorgiou


1 Answers

Looks like the swagger API at the time of writing is Base64 encoding NSData into the post. So avoid that if you don't want to use Base64 or if don't want to extend/modify the swagger generated code.

To send binary data looks like you need a NSURL of the local file.

like image 167
zingle-dingle Avatar answered Nov 18 '22 18:11

zingle-dingle