compose resources not loading on iOS ~ after 1.6.8
The documentation does not specify at all how to access (common) resources on a iOS side of a Compose Multiplatform project.
Describe the bug Ever since the new updates (1.6.10 and up), the new compose.resource { .. } ends up generating folders/resources that iOS cannot find or for some reason are not being loaded on iOS.
Affected platforms
- iOS
Versions
- Libraries:
- Compose Multiplatform version: 2.0.0
- agp: 8.5.0
- Kotlin version: 2.0.0
- OS architecture (x86 or arm64):iOS iphone simulator
- Device (model or simulator for iOS issues): iOS iphone simulator
To Reproduce Steps to reproduce the behavior: Say I need to display a logo on a MKMapview on iOS.
- Run this code snippet:
// before 1.6.8, it used to be -> val image = UIImage.imageNamed("compose-resources/logo.png") // with 1.6.8, it became -> val image = UIImage.imageNamed("compose-resources/drawable/logo.png") // now after +1.6.10 val image = UIImage.imageNamed("composeResources/compose-resources/resources/drawable/logo.png") // this now return null somehow // then i assign my image like this annotationView.image = image
I figure the path out only because that is where it ends up in the build folder, just like this image
2.make sure your compose.resources block in build.gradle.kt looks like :
compose.resources {
packageOfResClass = "resources"
}
Expected behavior the resource should be loaded without any issue just like with previous version Every since the update after 1.6.8, Compose stopped loading the resources and the path for generated resources on iOS seems longer
Please, advise how do I load this resource on iOS 100% kotlin ?
The documentation does not specify at all how to access (common) resources on a iOS side of a Compose Multiplatform project.
🤯
UIImage.imageNamed(
Res.getUri("drawable/logo.png")
.substringAfter(NSBundle.mainBundle.resourcePath!!)
.removePrefix("/")
)
UIImage.imageWithData(Res.readBytes("drawable/logo.png").toNsData())
@OptIn(ExperimentalForeignApi::class, BetaInteropApi::class)
fun ByteArray.toNSData(): NSData = this.usePinned {
NSData.create(bytes = it.addressOf(0), length = this.size.convert())
}
Thanks for the quick reply and pointing this out !
I've tried the getUri, and when I do this
...
val imagePath = Res.getUri("drawable/logo.png").substringAfter(NSBundle.mainBundle.resourcePath!!).removePrefix("/")
println(imagePath) // this output -> "composeResources/compose-resources/resources/drawable/logo.png"
the value of imagePath is exactly the same as in my above example ~ meaning it's pointing towards the correct path, however it's not loading/find the resource in there
Does Res.getUri display anything when you try it ?
@terrakok please take a look
@kocheick How do you integrate your kotlin code to the iOS app? Are you sure the resources are presented in the final app? If not, try to generate a new project and do the same
@terrakok Closing this, all is actually well with Compose. As I was trying to make a sample project to demonstrate this issue, I came to find out the reason my resources were not loading in the app was due to corrupted files ~ i had copied the logo image from one project to another within Intellij-idea and somehow the copy became a corrupted file (couldn't open it within the ide but no issue opening the original logo within the ide). I ended up making a copy within Finder from the original project to the new project and now Compose displays it as it should.
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.