We are creating a PDF viewer application fin the Xamarin platform. In that, we are adding a stamp annotation, which is added as a UIImage. While saving this UIImage, there is a loss in the quality of the image annotation. I have attached the example code below that causes the issue. Is there any way to improve the quality of the UIImage that we get from UIImageView?
I have attached an image that has been changed from 36kb to 18kb. The image is more pixelated than the initial image.
Assembly assembly = Assembly.Load(new AsemblyName("Syncfusion.SfPdfViewer.XForms.iOS"));
Stream stream = assembly.GetManifestResourceStream(string.Format("<path>.screen-shot.png"));
var imagedata = NSData.FromStream(stream);
if (imagedata != null)
{
image = UIImage.LoadFromData(imagedata);
}
UIImageView imageView1 = new UIImageView(image);
imageView1.Frame = imageView.Bounds;
var UiImageTest=ConvertToImage(imageView1);
using(var imageStream=UiImageTest.AsPNG().AsStream())
{
string path = "/Users/<path on MAC>";
string filepath = System.IO.Path.Combine(path, "StampParserTestjpegSave.png");
FileStream outputFileStream = File.Open(filepath, FileMode.Create);
imageStream.Position = 0;
imageStream.CopyTo(outputFileStream);
outputFileStream.Close();
}
private UIImage ConvertToImage(UIView view)
{
if (UIDevice.CurrentDevice != null && UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
var format = new UIGraphicsImageRendererFormat();
format.Scale = UIScreen.MainScreen.Scale;
var renderer = new UIGraphicsImageRenderer(view.Bounds.Size, format);
return renderer.CreateImage((rendererContext) =>
{
view.Layer.RenderInContext(rendererContext.CGContext);
});
}
else
{
UIGraphics.BeginImageContextWithOptions(view.Bounds.Size, false, 0);
view.Opaque = false;
view.DrawViewHierarchy(view.Bounds, true);
view.Layer.RenderInContext(UIGraphics.GetCurrentContext());
var img = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
return img;
}
}