1

I made UIViewControllerRepresentable to warp the UIActivityViewController into SwiftUI like this:

struct ShareSheet: UIViewControllerRepresentable {
   typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void
   let activityItems: [Any]
   let applicationActivities: [UIActivity]? = nil
   let excludedActivityTypes: [UIActivity.ActivityType]? = nil
   let callback: Callback? = nil

func makeUIViewController(context: Context) -> UIActivityViewController {
    let controller = UIActivityViewController(
        activityItems: activityItems,
        applicationActivities: applicationActivities)
    controller.excludedActivityTypes = excludedActivityTypes
    controller.completionWithItemsHandler = callback
    return controller
}

func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
    // nothing to do here
}

The problem is: when I pass UIImage object its works, like this:

ShareSheet(activityItems: [UIImage(named: "doc")!])

But if I pass Image object its not work, like this:

ShareSheet(activityItems: [Image("doc")])

In my App I have only images in Image object not UIImage, so, can I warp Image to UIImage Or I have to do something else?

Nayef
  • 463
  • 5
  • 13
  • It seems like Image will never be used to create a UIImage. I found this [explanation](https://stackoverflow.com/questions/57028484/how-to-convert-a-image-to-uiimage), here the answer created an Image from a UIImage however. – jnblanchard Jan 29 '20 at 22:33
  • I can't find it in the documentation (yet), but UIKit is not setup to handle SwiftUI items, hence the need to bridge the UIKit item via `UIViewControllerRepresentable.` . Like @jnblanchard indicated in his link above, your easiest answer looks like you'll need to convert those Images into UIImages to pass them. The documentation for Image also indicates that it is a late binding token which depends on its environment to resolve. If you give it a UIKit environment, I suspect it does not have the context to do so. https://developer.apple.com/documentation/swiftui/image – binaryPilot84 Jan 30 '20 at 02:58
  • Just read [Parameters section](https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622019-init) – Asperi Jan 30 '20 at 10:02
  • I think If I can warp Image to UIImage the problem will resolve! but how to do? – Nayef Jan 30 '20 at 10:44

1 Answers1

0

The reason is that Image("doc") is of type View which can't be processed by UIKit. On the contrary, UIImage(named: "doc") is of type UIImage, this is why it works.

Adrian
  • 415
  • 4
  • 18