Skip to content

iOS ui freezing when I call readBytes from 35 MB video file #195

Open
@kotlin-compose-multiplatform

Description

MY VIEWMODEL CODE (android app working fine):

 private suspend fun readFileBytes(files: PlatformFile?, onSuccess: (ByteArray?) -> Unit) {
        withContext(Dispatchers.IO) {
            if(files!=null) {
                val f = files.readBytes()
                onSuccess(f)
            } else {
                onSuccess(null)
            }
        }
    }

YOUR METHOD:

 @OptIn(ExperimentalForeignApi::class, BetaInteropApi::class)
    public actual suspend fun readBytes(): ByteArray = withContext(Dispatchers.IO) {
        memScoped {
            // Start accessing the security scoped resource
            nsUrl.startAccessingSecurityScopedResource()

            // Read the data
            val error: CPointer<ObjCObjectVar<NSError?>> = alloc<ObjCObjectVar<NSError?>>().ptr
            val nsData = NSData.dataWithContentsOfURL(nsUrl, NSDataReadingUncached, error)
                ?: throw IllegalStateException("Failed to read data from $nsUrl. Error: ${error.pointed.value}")

            // Stop accessing the security scoped resource
            nsUrl.stopAccessingSecurityScopedResource()

            // Copy the data to a ByteArray
            ByteArray(nsData.length.toInt()).apply {
                memcpy(this.refTo(0), nsData.bytes, nsData.length)
            }
        }
    }

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions