SVG Image in iOS-TabbedPage blurry
See original GitHub issueI’m trying to add a SVG image to a TabbedPage. It comes up very blurry though. If I try to compensate the size for the screen scale, it comes out way too big and get’s larger then the TabBar itself.
I tried rendering it in a higher resolution and then downscale it using UIImage.Scale
but it get’s even worse with that.
Here is my minified TabbedRenderer code:
protected override async Task<Tuple<UIImage, UIImage>> GetIcon(Page page)
{
int height = TabBar.Frame.Height - TabBar.SafeAreaInsets.Bottom;
int size = (int)(height / 2d);
var params = ImageService.Instance.LoadEmbeddedResource(((UriImageSource)page.IconImageSource).File)
.WithCustomDataResolver(new SvgDataResolver(0, size, false));
var image = await params.AsUIImageAsync();
return Tuple.Create(image, (UIImage)null);
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
Top Results From Across the Web
In Safari svg mask looks blurry like if it's raster
Safari has some problems with resizing SVG images, in the case the viewBox attribute is missing. I can't tell if this is an...
Read more >Issue with blurry SVG images on mobile - General
It is an iOS issue that occurs when an SVG file is smaller than 20px. Even though it's vectors these browsers have issues...
Read more >[Resolve]-SVG icons in safari are blurred - appsloveworld.com
I have noticed that svg icons that placed via 'img' tag aren't rendered correctly in safari. They end up being all blurry. I...
Read more >Developers - SVG Image in iOS-TabbedPage blurry -
I'm trying to add a SVG image to a TabbedPage. It comes up very blurry though. If I try to compensate the size...
Read more >6 Common SVG Fails (and How to Fix Them)
I'm editing these to reframe the image. The last two are the width and height of the coordinate system inside the viewport —...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Possible related to #1305.
I had a similar problem when using SvgImageSource (might also be valid for other sources):
The problem is that scale is ignored in the IImageSourceHandler for iOS. This property should be passed when instantiating the UIImage. I did a quick test and registered my own IImageSourceHandler and everything worked:
@daniel-luberda I would go for a PR, but I’m not sure where this parameter should be added and passed down to the SvgDataResolver.
@Dresel thak you for your help. I finally succeeded in implementing this. This is what I’ve done :
1- Add this class based in iOS project, based on Dresel post :
public class ScaleAwareImageSourceHandler : IImageSourceHandler { public ScaleAwareImageSourceHandler() { WrappedImageSourceHandler = new FFImageLoadingImageSourceHandler(); }
2- In AppDelegate.cs, use custom implementation of InitImageSourceHandler() :
//CachedImageRenderer.InitImageSourceHandler(); //Use a custom ImageSourceHandler for Svgs. ScaleAwareImageSourceHandler.InitImageSourceHandler();
3- Keep scale to use it later. On Android, scale is always 1. But on iOS it can be found in ScaleHelper.
ScaleAwareImageSourceHandler.InitImageSourceHandler(); … //ResourceHelper is my helper class. You can use your own. ResourceHelper.Scale = (float)ScaleHelper.Scale;
4 - Use the scale when building the image :
SvgImageSource.FromResource(resourceName, vectorWidth: (int)(width * ResourceHelper.Scale), vectorHeight: (int)(height * ResourceHelper.Scale)
That’s all !