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.
Related
Blazor Web assembly has a convenience method that converts an IBrowserFile containing an image into a resized version - which is handy for resizing large images prior to uploading them.
This method takes a format as a string which determines the format of the output file.
Is there anywhere a list of valid formats that this property will accept? Can you specify the compression or bit depth values on the resulting file?
Currently, If I take an existing .jpg file and convert it using a format string of "jpg" the resulting file, although smaller in pixel dimensions is actually about double the size on disk. A 4000x3000 image at about 2.8MB can be "reduced" to a 2000x1500 image that's 7.7MB in size. Which is obviously not helping when the purpose is to reduce upload size. I could easily upload the 2.8MB file and resize it more efficiently on the server.
var imageFile = await file.RequestImageFileAsync("jpg", 2000, 2000);
This suggests I'm using the method incorrectly - but Microsoft's documentation on this method gives no clues as to what valid "Format" strings might, only stating that it is a string type. I've tried ".jpg", "JPEG", "jpg" - all of which seem to produce the same valid jpg file. What should I be passing here to actually reduce the file size?
See https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types
It's actually not "image/jpg" but "image/jpeg". If you specify non-existent format, the fallback (at least for me) seems to be "image/png". That's why you always got a valid image but with the same filesize.
I think this method uses html types:
html types
Try "image/jpg".
Be careful, though, this is a request to the browser, and the browser can send back whatever it wants. I believe this will work fine on all browsers, but you'd better check some of the common culprits (Hi, Opera!) to confirm.
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'm currently working on a ruby on rails project. In the project I have a form with a input file type (An image) and I need to convert the image to base64 (The project connects to an external api, so the image needs to be in base64)
So far I have tried to do this Base64.encode64(target_params[:image].read)
but I get an empty string as result.
Just solve it using this code:
file =
target_params[:image].tempfile.open.read.force_encoding(Encoding::UTF_8)
Base64.encode64(file)
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'm looking for the best way (or easiest) to import data into my iOS app using Swift. I've got a file containing recipes and I have to read in the recipe names and instructions.
Example:
Fudge brownies
Mix ingredients in processors until consistent.
Prepare baking sheet with coconut oil and set over at 425.
....
So I have to import several dozen recipes, my questions are
Would it be best to read in a text file?
If so how is this done in Swift?
Also how do I avoid issues with reading the title and recipe into separate variables?
You can read in a text file quite easily doing something like this:
let path = NSBundle.mainBundle().pathForResource("fileName", ofType: "txt")
var dataString = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)
Note you'll have to import Foundation.
You could then create a method to parse the dataString, something like
func parseDataString(string: String)
which you could send the dataString to.
You could put markers (e.g. special characters like (*) ) in the text file that would allow this method to figure out where the titles end and the directions start. There are a number of ways it could be done.
You could then persist your data using CoreData.
I would strongly suggest using JSON data in the files. JSON is a very simple markup format that gives structure to text files, and lets you basically say things like title=BBQ ribs. The reason you use JSON is that Cocoa has really good JSON handling built right in. Check out this thread, it probably does exactly what you want...
How do I parse JSON with Objective-C?