How to use SVG images for Xamarin.Forms application? - xamarin.forms

It seems it's possible to some extent to use SVG images in some controls, for instance SVG files can be only added as embedded resources when using some of the available NuGet packages out there. Also, at least for ToolbarItem the images need to be stored somewhere in the file system, so I had to create my own SVG rasterizer that saves those images to bitmaps in the cache directory, so those could be passed in a FileImageSource to the Icon property in the control ToolbarItem. For, the control Button those images need to be stored in the Resources or drawable directory, so it won't work with the rasterized bitmaps, since those are stored in the cache directory. With all those limitations when trying to use SVG files directly at runtime, I was thinking maybe I could convert all those images to the corresponding JPG or PNG images at build time using some task, but I'm not sure whether that conversion could be done with the current available tools. So, my questions is how to use convert directly SVG images for using in common controls in a Xamarin.Forms application (ToolbarItem, Button, etc)?

Related

Symfony / CKEditor / Elfinder : is it possible to convert automatically uploaded image png to jpg?

From Symfony 5, I installed and configured the CKEditor buundle and attach it to the Elfinder bundle.
All works perfectly. Now, for increase the users experiences, I would like convert automatically any png file updated via elfinder to jpg file (for save disk space usage ;) )
Despite my researches, I can't find if there is a CKEditor and/or Elfinder way for upload image.
Nothing in the documentation. I also look the result of the php bin/console config:dump-reference fm_elfinder command, but I don't find anything about a conversion...
Does that mean there isn't a quick/provided way for do what I'm looking for ?
Thanks for any help :)
Are you sure that it will increase the users experiences to convert their png to a jpg image? I don't think so, you will loose a lot of functionalities of png (transparency, color protection, etc.). Do not forget that png image can be compressed too!
If you are sure that, in your case, it will increase the users experience, this bundle does natively not do it. You'll have to code. I saw two solutions:
Solution1: FMElFinderBundle comes with a plugin to resize image. You could perhaps have a look on it to create a new specification which will transform png into jpeg instead of resize it.
Solution2: FMElFinderBundle use the FileSystem Symfony component to store image. This bundle allows developers to change this component (If you want to replace it by an Amazon service as example). You could create your own component by overriding the Symfony filesystem. Just replace the dumpfile or the copy methods. If file is a png, you call the gd2 library to convert it.

Loading images from a resource folder or from url?

In a project that use 300 or more small icons which is a better method or is there any difference if you load the icons directly from the url in a folder or adding them in resource file and then use them in the project.
Using a resources way a size of an executable is bigger and you cannot change icons without recompiling the source code. But you may be sure that all the icons are in place.
And vice versa for the url/folder approach.

Style in SVG, not applied on the browser (React)

I'm trying to add a SVG into my webpage, I think the format is a bit odd.
https://gist.github.com/Vadorequest/c329dec26e39a586e96df5f74c1d7d29?short_path=d29c6c9 (you can see the source code and the rendering there)
The style part isn't correctly understood since I'm loading this file using React. If you save the file and open it in a browser, it'll work fine. But if I load it by react using react-svg-loader, it displays a dark image.
import IconBook from "-!react-svg-loader!../assets/couverture-eBook-VF.svg";
...
<IconBook height={250} />
Is it standard to put the style like this? Should I get a new SVG exported differently? I really don't know that format and all its possibilities.
Edit 1:
I found a working workaround for anyone interested: https://github.com/gilbarbara/react-inlinesvg
It basically loads the svg file over network and anything in it will be correctly loaded. (including <styles>)
On the bad side, it makes the app rely on something that must be available via CDN or alike. (you won't have the svg embedded in your app. If you wanted some kind of standalone app that can be built with everything in it, it's not really good)
On the good side, it doesn't require any change in the SVG file, you use it as it, load it over network, and it displays as in the browser.
Since I'm building a standalone app (basically, npm run build will generate a folder with my whole app) this isn't perfect because I need to host that file somewhere on a CDN. So I'll keep looking for another solution.
It is fine to have styles in your svg file, as specification says:
Cascading Style Sheets (CSS) is a stylesheet language used to describe
the presentation of a document written in HTML or XML (including XML
dialects such as SVG or XHTML). CSS describes how elements should be
rendered on screen, on paper, in speech, or on other media.
https://developer.mozilla.org/en-US/docs/Web/CSS

JavaFX Override Loading images from CSS

Is it possible to set how JavaFX loads images from CSS? If I could delegate this to a certain utility class.
In Swing I had an ImageStore utility class which loaded the images needed by all the components. The ImageStore retrieved the image from a given URL and cached it to disk for later retrieval. It also used in memory cache for running application to avoid touching the disk on every retrieval. This way if 100 components wanted the same image, the image was only fetched 1 times, and not 100 times.
I have a dilemma now with using CSS for styling
-fx-background-image:url('/images/single-search.png')
How to delegate this loading to ImageStore?
How does JavaFX handle loading of images defined in CSS?
If JavaFX CSS does some caching on their own, this will only solve one problem. It will not fetch the same image multiple times. However my ImageStore saves all images to file cache where they are also available the next time the application is started a new.
JavaFX uses it's own cache class:
com.sun.javafx.css.StyleManager contains the Image getCachedImage(String url) method that is used by the converter used for the background image css properties. (Note that this class is not part of the public API.) This class does not seem to write the images to files and neither is there a way to replace the way the images are loaded in a clean way (i.e. not using reflection or similar means), but keeps references to the images used in memory...
Of course you could replace the CssMetaData object responsible for the background properties in the List returned by the getCssMetaData() method with a CssMetaData object using your own cache, but this would require extending every subclass of Region you want to use your cache with and using those classes instead of the standard Regions...

Embed image in flex? Export -> release build not working.

I've a flex application which has actionscript file, images. When i run this in adobe flash builder, everything works. When i export it as a release build and run in other file, swf does not show images and interactivity of flex components is also lost.
Is there a specific way to embed images?
Here is image code,
I don't see your code, but I've had similar issues before. The trick is to reference the image starting with a forward slash '/' in the path. For example, if the image is located at 'img/test.png' then use '/img/test.png' as the embed source path. You should also check to make sure the image is included in your build path.

Resources