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)
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.
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.
I have generated an image in my app an I would like to share it via UIDocumentInteractionController which takes an argument in form of NSURL. Of course I don't want do directly convert image data into a URL, but what is the best way to achieve this? Can you temporarily store that image somewhere and then get the url and delete it afterwards after UIDocumentInteractionController has done it's job? Or convert it to NSData somehow and then get the url of the NSData object? (I tried this and failed btw)
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
I have successfully returned a thumbnail() request (using the Dropbox SDK) in my rails app, but I don't understand how to process the response. I would like to show the thumbnail on a webpage.
I also tried to save the response to a tmp file, but get a UndefinedConversionError ("\xFF" from ASCII-8BIT to UTF-8) error.
I'm actually doing exactly what you are asking for. What I did was to convert the returned bytes into a base64 string. In C# it's quite easy as there is a convert function to do that.
On the webpage you then have to set the src attribute of a img tag to
<img src="data: image/jpg;base64,PlaceBase64StringHere"...../>
There is a little overhead in the converted string, but it's very easy to handle and you use the power of the client browser to render the image.