I know how to convert the common NSAttributedString (which has no image) into HTML. I set the Images as NSTextAttachment to NSAttributedString (these attributed strings are set as attributeText of a UITextView). How could I convert the whole attributed string into HTML?
I‘ve found an answer related solution,which mentions:
Simple idea for image: encode it with base64 and put it directly in a < img > tag with the right frame.
But how could i implement that?
Related
I have this image which I transformed into base64. The issue is the string that it gave me is so huge, I'm not able to see any other lines of codes. I inputted the given string in the source tag and it works the image. How do I shorten the base64 data of the image in order to see other lines of code?.
[an example of the string (couldn't fit the whole screenshot as it's so big)]
Was expecting the base64 string to be short in order to put it in the tag.
I have some trouble in understanding how does html convert base64 to img
From https://www.lucidchart.com/techblog/2017/10/23/base64-encoding-a-visual-explanation/ I learn how string convert to base64,but what dose it happen when base64 encoding to rgb
Base64 is a way to encode binary data as text (so it can be put anywhere you can put text, such as in an HTML attribute value).
The browser converts the Base64 data into whatever form it was in originally.
If that was a PNG it converts it back to a PNG. If it was a JPEG then a JPEG. And so on.
The browser doesn't convert the Base64 data directly to pixel values at all.
I am using angular 7 and need to upload image and send it to server to put it in database. So i wondering how to convert image into string and latter back in image so i can display it in app?
This SO answer explains how to convert an image into base64 encoded data.
You can use the HTML5 for it:
Create a canvas, load your image into it and then use toDataURL() to
get the base64 representation (actually, it's a data: URL but it
contains the base64-encoded image).
I don't think that's how you want to save your images though. Continue researching perhaps.
In my NSString, I've some accented characters and html tags, such as <i></i>. I want to display them in NSTextField with text attributes to them.
When I build the attrStr, I use:
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithHTML:[text dataUsingEncoding:NSUTF8StringEncoding]
options:#{NSWebPreferencesDocumentOption : webPreferences}
documentAttributes:NULL];
I display the text as follow:
someTextField.attributedStringValue = attrStr;
But this require I know the encoding of the NSString, but the problem is that I do not know the encoding. So how do I build an attribute string without knowing the encoding?
You don't need to know the encoding of your NSString – it is an Objective-C string. The definition of dataUsingEncoding: states, emphasis added:
Returns an NSData object containing a representation of the receiver encoded using a given encoding.
This method converts from whatever encoding NSString uses internally into the specified encoding – so the result is your case is an NSData containing the bytes of a UTF8 string.
If you are seeing invalid results maybe the issue is when you create the original NSString?
HTH
Addendum I now see you asked this question first (I added an answer – basically you need to know or guess). Why are you reading the file into an NSString in the first place? Just read it directly into an NSData and then let the HTML parser sort it out (it will either read the HTML encoding tag or default to an encoding).
Eventually, I found that if I use NSUnicodeStringEncoding on the above, the accented chars will be displayed correctly.
When I try to show the JSON string, the indentation is gone and very ugly.
Is there a way to make it look better?
As you stated in a comment : the JSON string looks good in a browser. That is propably (I'm 99% sure) because most browsers will recognize the content as JSON and display it nicely, in an easier to read form. But in its core, the JSON string is just a set of characters, without any special whitespace characters like /n or /t to indicate newlines or tabs.
So to answear your question : in order to display your JSON string similarily to what you see in your browser, you'd have to parse and format it yourself.
If you are using a CCLabel to display it, you could subclass it creating a JSONLabel which would format a string given in its constructor the way that you like it.