skiptools / skip-ui

SwiftUI for Android
https://skip.tools
GNU Lesser General Public License v3.0
119 stars 12 forks source link

PDF assets seem to not work in Assets Catalog #32

Closed hepspl closed 4 months ago

hepspl commented 4 months ago

One of the formats supported by Assets Catalog is PDF. This is a very good way of putting vector images so Xcode will automatically prepare rasterized (or preserve vector) images for various screens densities at build time.

However it seems that SKIP is not handling PDFs well (even if xcode is rasterizing them). I am unable to see any image supplied in PDF format rendered on Android app (iOS works ok).

I hope you would address this issue as I find this crucial when working with images in apps.

marcprux commented 4 months ago

This would be a great feature to have! The image library we are using (Coil) doesn't have direct support for PDF, but I suspect that a custom image loader could be added to it using Android's native PdfRenderer , which could just convert the first "page" of the PDF into a bitmap. Then the image loaded could be added to Coil in much the same way as we already add the JarURLFetcher in AsyncImage.swift.

This would be a great first project if someone is interested in contributing!

marcprux commented 4 months ago

I've gone ahead and implemented this in skip-ui 0.9.4. You can now put PDFs in the asset catalog for the top-level module, and you will be able to load it by name. Be sure to use the Image(name, bundle: .module) syntax, since the asset catalog needs to be added to the top-level SwiftPM Resources/ folder so it can be handled by the Skip transpiler.

If you mark the PDF asset with "Preserve Vector Data", it will use the foregroundStyle to tint the image, just like on iOS. Otherwise, the colors in the PDF will be used.

You can get the latest version with File/Packages/Upgrade to Latest Package Versions. Please let us know if you run into any issues with this new feature.

Screenshot 2024-05-29 at 11 58 54
hepspl commented 4 months ago

How awesome! It works! Thank you for this!

However I don't see any difference between "Preserve Vector Data" enabled or disabled. Each time I can see on Android that image is rastered and using original colors.

marcprux commented 4 months ago

However I don't see any difference between "Preserve Vector Data" enabled or disabled.

The colors are actually supported using the "Render As" asset setting. "Template Image" will allow you to set the foregroundStyle.