You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Compose MultiPlatform (CMP) iOS端截图红蓝色通道互换问题的解决方案咨询

Compose MultiPlatform (CMP) iOS端截图红蓝色通道互换问题的解决方案咨询

各位好,我现在在开发Compose Multiplatform(CMP)的iOS端应用,遇到了一个头疼的截图颜色异常问题:Compose UI里设置为红色的错误文本(用MaterialTheme.colorScheme.error(Color(0xFFEE504F))定义),截图保存到相册后变成了蓝色(对应色值Color(0xFF4F50EE)),看起来明显是红、蓝通道的像素数据被互换了。

我当前的截图与转换实现

我现在用Kotlin/Native调用UIKit API来截图Compose View:

val renderer = platform.UIKit.UIGraphicsImageRenderer(composeView.bounds)
val image = renderer.imageWithActions { ctx -> 
    composeView.drawViewHierarchyInRect(composeView.bounds, true) 
}

之后把生成的UIImage转换成Compose的ImageBitmap:

fun UIImage.toImageBitmap(): ImageBitmap {
    val pngData = UIImagePNGRepresentation(this)!!
    val byteArray = ByteArray(pngData.length.toInt()).apply {
        usePinned { 
            memcpy(it.addressOf(0), pngData.bytes, pngData.length) 
        }
    }
    return Image.makeFromEncoded(byteArray).toComposeImageBitmap()
}

我猜测问题出在这个转换流程里,导致ImageBitmap的红、蓝通道数据被交换了。

我已经尝试过的方法

  • 试过使用UIGraphicsBeginImageContextWithOptions,但该API在Kotlin/Native环境下无法找到
  • 尝试了其他UIKit和CoreGraphics的相关API,但大部分在Kotlin中都是未解析的引用
  • 受项目限制,我不能使用任何iOS专属第三方依赖,也不允许进行Swift/Objective-C互操作

我的需求

有没有办法在不依赖Swift/Obj-C代码、不引入第三方库的前提下,用纯Kotlin/Native代码正确截取Compose UI的截图,解决红蓝色通道互换的问题?

感谢各位的帮助!

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 08:19:30