Image quality decreases while saving UIImage

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;
            }
        }

You seem to be starting and ending with PNG data, so the only thing I can imagine is you are somehow reducing the size of the image. But its curious why you are going through this process of loading the image into an image view in order to render it out again? The most optimal thing would (given this code) be to simply preserve the original image data. At worst if you need to resize the image, you could do so without using UIImageView at all.

In general my recommendation here would be to check that all the sizes and scales you are seeing are what you expect along the way, but given what the code you have here would likely be expected to produce, I would spend time deleting most of it and just saving the data you downloaded instead.

Hi As I mentioned earlier, we are trying to do custom stamp annotation on a pdf viewer, for which we need to convert the image stream from the pdf to PNG data, and to show them on the viewer, we need to convert it into a UIImageView. The user might resize or may not resize at all, but this conversion is necessary to view the image. The viewed image must be further saved, so it will be converted to PNG again in order to save as a PDF stream again. The code mentioned before has a few steps where we were able to replicate the issue of degradation of the image quality.

You don't need to use a UIImageView in order to redraw the image – UIImage has API to directly draw the image (and thats likely what you should be using, with the destination as a PDF context, if you want to annotate the image). You can similarly draw text directly, you don't need (for example) a UILabel. And if your ultimate destination is a PDF file, then CALayer's renderInContext method is a poor choice, as it will render what is on screen – which is going to be a bitmap and generally will bloat PDF files while also producing a lower quality result.

Effectively, if your output is PDF and you want good quality, you do not want to use UIViews to hold the interstitial rendering, because they are optimized for screen presentation, and will be lower quality in a PDF file.

Image quality decreases while saving UIImage
 
 
Q