Search code examples
iosswiftpdfuiimageviewuiimage

Swift - image quality loss after import


I am having a problem with my images in Xcode. I always use SVGs and convert them to PDFs. There should be no pixels. Unfortunately my pictures lose a significant amount of quality after loading them in my app.

The only thing I do is changing their width/height and I often make them a lot smaller than the actual image is. Here is a example:

enter image description here

Both logos are actually PDFs and perfect quality. The only thing I change is the size (while maintaining aspect ratio. Does anyone know why this quality loss occurs and how I can fix it ?


Solution

  • PDF's are rasterized into PNG's at compile time unless you request otherwise. The rasterized PNG will then be downsampled when you request a smaller image and the result is the poor sampling you are seeing. To keep the pdf as a vector in the asset catalog select your pdf and in the attributes inspector on the right make sure you choose Scale: Single Scale and check the box for Resizing: Preserve Vector Data. This will cause the actual pdf to be bundled with he app and it will be rendered at the correct scale at runtime, instead of the compile time rasterized png.