moko-resources icon indicating copy to clipboard operation
moko-resources copied to clipboard

toUIImage returns nil for iOS

Open Danijal98 opened this issue 1 year ago • 7 comments

I am using the version: 0.23.0 I have the following structure for my images: shared/commonMain/resources/MR/images/ic_partnership.svg

I have a suggested helper method

fun getImageByFileName(name: String): ImageResource {
    val fallbackImage = MR.images.transparent
    return MR.images.getImageByFileName(name) ?: fallbackImage
}

From iOS I call it like this

let imageRes = ResourcesKt.getImageByFileName(name: "ic_partnership") // Successfully finds the resource
let uiImage = imageRes.toUIImage() // returns nil
Image(uiImage: uiImage!).padding() // crashes because "Unexpectedly found nil while unwrapping an Optional value"

Danijal98 avatar Feb 02 '24 22:02 Danijal98

Could it be that this an issue with svgs? .toUIImage seems to work fine for me with pngs, but with svgs I get nil as well.

rph8 avatar Feb 21 '24 14:02 rph8

svg works in samples. svg on iOS require iOS 13+, check your iOS version please

Alex009 avatar Feb 25 '24 16:02 Alex009

svg works in samples. svg on iOS require iOS 13+, check your iOS version please

I am using the iOS 17, so I think that should not be the issue

Danijal98 avatar Feb 27 '24 09:02 Danijal98

@Danijal98 please test your svg inside xcode project directly. maybe iOS for some reason can't process this svg

Alex009 avatar Feb 27 '24 14:02 Alex009

I’m seeing a similar issue with an SVG file on iOS. Interestingly, while in light mode, I see the image (.toUIImage() returns non-nil). However, when I switch to dark mode, I don’t see the image (.toUIImage() returns nil). Maybe this clue helps?

djehrlich avatar May 13 '24 19:05 djehrlich

I’m seeing a similar issue with an SVG file on iOS. Interestingly, while in light mode, I see the image (.toUIImage() returns non-nil). However, when I switch to dark mode, I don’t see the image (.toUIImage() returns nil). Maybe this clue helps?

Is it maybe related to this one (should be fixed in the most recent version / beta)? https://github.com/icerockdev/moko-resources/issues/543

rph8 avatar May 13 '24 22:05 rph8

I’m seeing a similar issue with an SVG file on iOS. Interestingly, while in light mode, I see the image (.toUIImage() returns non-nil). However, when I switch to dark mode, I don’t see the image (.toUIImage() returns nil). Maybe this clue helps?

Is it maybe related to this one (should be fixed in the most recent version / beta)? #543

Dark mode SVG images are indeed working. The comments for #624 provided the insight I needed - add an image resource with “-dark” appended to its name. I tested by copying “car_black.svg” to “car_black-dark.svg” and both light and dark mode now work as advertised.

djehrlich avatar May 13 '24 23:05 djehrlich

@Alex009 I can confirm that this bug does not occur anymore for me after upgrading to moko 0.24.1

Danijal98 avatar Jun 24 '24 13:06 Danijal98