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?
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.
Related
i read this post convert image to base64 with flutter but this is about converting image file to base64. How to convert online url to base64.
Right now i can think of one solution that is to store image in path and get that file path and convert it. thats how this post showing.
is there any other sort way to convert online image uri to base64?
Well to convert the image you need the image data otherwise there's nothing to convert. So the step for the most effecient would be.
Perform get request to the image url
Read all the bytes from the response body
Convert to base64
Save the base 64 string locally or use how you please.
You don't have to save it. Just keep in mind if the user closes the app or it stops at any point during this process you'll have to start it from the beginning because you're not saving the image to disk.
Given a Hash of string => string, and string => file entries, how do I create a multipart/form-data string?
After seeing many questions like this in forums all over the web back up to 2008, I finally found the solution:
Rack::Test::Utils.build_multipart({foo: 'bar'}, true, true)
The first argument is for the data, the second to specify whether it's the first multipart and the last - and most important - is to force it to use multipart even for parameters that don't strictly require it (for example because there's no file upload happening).
I'm working on a project using Swift. For it, I have to create a JSON file, which should also contain images. Now, I've tried to add Images to Xcode's Assets catalog and then create from them a Base-64 encoded string (I would like their format in a JSON to be a String). I use this code for one image to create the string:
let image = UIImage(named: "restaurant")
let imageData = UIImagePNGRepresentation(image!)
let strBase64 = imageData?.base64EncodedString(options: .endLineWithLineFeed)
When I encode this to JSON, it works normal. The problem is, in the future I won't have a JSON file inside of my project. It will be requested from the server, so when I manually add the Base-64 encoded string of my image to the JSON (first, I type it on the console, then, just copy it to JSON), my JSON file becomes terribly long (more than 2000 lines for just one image). After looking at the structure of different JSON files, used in tutorials, I've noticed that for images, the authors of those files use url strings. So, I wonder, whether it is the default way to add images to JSON and I shouldn't try to copy the whole Base-64 encoded string of my image? This is the first time I'm trying to add an image to a JSON, so I would appreciate any suggestion and advice.
I need to be able to access the raw image data from a loaded photo. Originally we were supposed to use Base64, however the API creator has changed it to raw image data.
In SWIFT, how would I accomplish the following?
I'm currently using Alamofire for my networking, but I'm not sure that it will work for this part of the API.
I already have the image resource as a variable to access, I just need to know how to get the raw data, and then formulate a POST request with just that data as the body of the request. A method with a callback would be awesome.
UIImagePNGRepresentation
UIImageJPEGRepresentation
These method can encode a UIImage to NSData.
upload code:
Alamofire.upload(.POST, "http://your.org/upload", data: imageData)
I am currently working with the 3rd party library SCRecorder to try and create an AVAsset with a URL and then create an AVPlayerItem -> AVPlayer to output a video.
I believe my problem arises because the video data I am trying to play is originally being saved to Parse as a raw data file. The reference URL to this file is in the format "http ://files.parsetfss.com/something-file". It does not appear that there is a way to create an AVAsset, and ultimately an AVPlayer, using this type of URL.
My question is whether or not there is a way to create an AVPlayerItem/AVPlayer using this URL which returns raw data, or create one using the raw data itself as an NSData? If this is not possible is there a way to save a PFFile to Parse in a different format, one that would be accepted by AVAsset/AVPlayer such as .mov/.mp4?
I figured out the issue... I just needed to use PFFile(data: data, contentType: "video/mp4") :P