Hey, guys and girls, I’m trying to implement image picker from photo library and camera with compos...
b

Babewmail

about 2 years ago
Hey, guys and girls, I’m trying to implement image picker from photo library and camera with compose multiplatform for Android and iOS. In Android everything works great. I have a problem with iOS: I have an expected composable function in the common part :
@Composable
expected fun ImagePickerView()
in the iOS shared part it is :
@Composable
actual fun ImagePickerView() {
    var bitmap by remember { mutableStateOf<ImageBitmap?>(null) }
    Box {
        Column(
            modifier = Modifier
                .align(Alignment.BottomCenter)
                .padding(bottom = 32.dp),
            horizontalAlignment = Alignment.CenterHorizontally,
        ) {
            bitmap?.let {
                Image(bitmap = it, contentDescription = null)
            }
            Button(
                onClick = rememberOpenPickerAction { image ->
                    bitmap = image
                },
            ) {
                Text(text = "Select Image")
            }
            Button(
                modifier = Modifier.padding(top = 16.dp),
                onClick = rememberOpenCameraAction { image ->
                    bitmap = image
                },
            ) {
                Text(text = "Take photo")
            }
        }
    }
}


@Composable
fun rememberOpenPickerAction(onImageSelected:(ImageBitmap) -> Unit): () -> Unit {
    val uiViewController = LocalUIViewController.current
    val cameraDelegate = remember {
        object : NSObject(), UIImagePickerControllerDelegateProtocol, UINavigationControllerDelegateProtocol {
            override fun imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo: Map<Any?, *>) {
                val uiImage = didFinishPickingMediaWithInfo[UIImagePickerControllerEditedImage] as? UIImage
                uiImage?.let { onImageSelected(it.toImageBitmap()) }
                picker.dismissViewControllerAnimated(flag = false, completion = {})
            }
        }
    }

    return remember {
        {
            val cameraController = UIImagePickerController()
            cameraController.setSourceType(UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypePhotoLibrary)
            cameraController.setAllowsEditing(true)
            cameraController.setDelegate(cameraDelegate)
            uiViewController.presentViewController(cameraController, animated = true, completion = null)
        }
    }
}

@Composable
fun rememberOpenCameraAction(onImageSelected:(ImageBitmap) -> Unit): () -> Unit {
    val uiViewController = LocalUIViewController.current
    val cameraDelegate = remember {
        object : NSObject(), UIImagePickerControllerDelegateProtocol, UINavigationControllerDelegateProtocol {
            override fun imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo: Map<Any?, *>) {
                val uiImage = didFinishPickingMediaWithInfo[UIImagePickerControllerEditedImage] as? UIImage
                uiImage?.let { onImageSelected(it.toImageBitmap()) }
                picker.dismissViewControllerAnimated(flag = false, completion = {})
            }
        }
    }

    return remember {
        {
            val cameraController = UIImagePickerController()
            cameraController.setSourceType(UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera)
            cameraController.setAllowsEditing(true)
            cameraController.setDelegate(cameraDelegate)
            uiViewController.presentViewController(cameraController, animated = true, completion = null)
        }
    }
}
With rememberOpenPickerAction everything works great, but with rememberOpenCameraAction, the delegate function imagePickerController is never called. I’ve researched a lot in internet and I’ve found that the problem may be from the delegate function, because in some version of Swift (Swift 5 I think), Apple had changed the function to be: optional func imagePickerController( _ picker:
UIImagePickerController
, didFinishPickingMediaWithInfo info: [
UIImagePickerController
.
InfoKey
: Any]) The difference is in the NSDictonary didFinishPickingMediaWithInfo to be with key of type
UIImagePickerController
.
InfoKey
and not Any. But this function isn’t implemented in the UIImagePickerControllerDelegateProtocol. Has anyone faced this problem? I’ll be happy if somebody share how to get the image(UIimage) after the Camera controller.
4
I'm trying to add a dependency on a Pod library from the CocoaPods repository to a KMM project, but ...
j

jimmyt

about 4 years ago
I'm trying to add a dependency on a Pod library from the CocoaPods repository to a KMM project, but I believe that Gradle is somehow causing my Podfile to be read improperly, almost as if the 'pod' command is ignoring it. When I attempt to sync the Gradle project, I get the following error and warning:
Generating workspace in `shared/build/cocoapods/synthetic/IOS/shared`
[!] The following Swift pods cannot yet be integrated as static libraries:

The Swift pod `web3swift.pod` depends upon `secp256k1.c`, `keccak.c`, and `scrypt.c`, which do not define modules. To opt into those targets generating module maps (which is necessary to import them from Swift when building as static libraries), you may set `use_modular_headers!` globally in your Podfile, or specify `:modular_headers => true` for particular dependencies.

[!] Automatically assigning platform `iOS` with version `13.0` on target `App-iOS` because no platform was specified. Please specify a platform for this target in your Podfile. See `<https://guides.cocoapods.org/syntax/podfile.html#platform>`.
The CocoaPods section of my build.gradle.kts file (which is located in "Project/shared") looks like this:
version = "1.0"
ios()
cocoapods {
    summary = "A summary to make CocoaPods happy"
    homepage = "A page to make CocoaPods happy"
    ios.deploymentTarget = "13.0"
    pod("web3swift.pod") {
    }
    useLibraries()
    podfile = project.file("../iosApp/Podfile")
}
And my Podfile (which is located in "Project/iosApp") looks like:
source '<https://cdn.cocoapods.org>'
use_modular_headers!
target 'App-iOS' do
    platform :ios, '13.0'
end
Any idea why I am still getting this error and warning, even though I have clearly implemented the fix in the Podfile? Any help is much appreciated.