Binding UIImage from Image

Hello,


there it's easy to convert a UIImage to an Image. But how I can convert an Image? to an Binding of UIImage?


Thank you

DWD


Could you show the code where you get the error mesage ?


That will make it much easier to explain the solution.

Hello Claude,


so, I made an Example. The Error occures, wehn teh view "WorkOnPicture" with the Binding will be called. Only the struct ContentView should be modified...


import SwiftUI
import Combine

class GetPicture: ObservableObject {//get picture from everywhere
 static let shared: GetPicture = GetPicture()
 let willChange = PassthroughSubject<Image?, Never>()
 @Published var image: Image? = nil {
  didSet {
   if image != nil {
    willChange.send(image)
   }
  }
 }
 func get() { 
  image = Image("ExamplePNG")
 }
}

struct WorkOnPicture: View {
 @Binding var image: UIImage
 var body: some View {
  Text("Hello Picture")
 }
}

struct ContentView: View {
 @State var image: Image? = nil
 var body: some View {
  VStack {
     image?.resizable().frame(width: 100, height: 100)
     .contextMenu {
      Button(action: { self.workWithPicture() }) {
        Text("Edit")
        Image(systemName: "pencil")
       }.disabled(image == nil)
   }
  }.onReceive(GetPicture.shared.$image) { image in
    self.image = image
   }
  .onAppear() {
   GetPicture().get()
  }
 }
 func workWithPicture() {
  WorkOnPicture(image: image) //<- here is the problem
 }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


Kind regards

DWD

I modified as follows.


It compiles, but I cannot do anything on the view (no image shown).

On line 28 I just get a warning : -> Result of 'WorkOnPicture' initializer is unused


struct WorkOnPicture: View {
    @Binding var image: Image!
    var body: some View {
        Text("Hello Picture")
    }
}

struct ContentView: View {
    @State var image: Image? = nil
    var body: some View {
        VStack {
            Text("Just to see Something")
            image?.resizable().frame(width: 100, height: 100)
                .contextMenu {
                    Button(action: { self.workWithPicture() }) {
                        Text("Edit")
                        Image(systemName: "pencil")
                    }.disabled(image == nil)
            }
        }.onReceive(GetPicture.shared.$image) { image in
            self.image = image
        }
        .onAppear() {
            GetPicture().get()
        }
    }
    func workWithPicture() {
        WorkOnPicture(image: $image) //<- here is the problem 
    }
}

Hello Claude,


this was only for demonstration, not for work.


but this not the solution, to change the data type from UIImage to Image, because I need it (datatype UIImage) for further procession in an UIKit View (datatype of struct WorkOnPicture).


Greeds

DWD

Problem is that you have inconsistent declaration: somewhere Image, elsewhere UIImage, cannot work this way.


I have tried to create an observed object, but does not change.


struct ContentView: View {
//    @State var image: Image? = nil
    @ObservedObject private var theImage = GetPicture()

Hello Claude,


so you see also no way to convert an Image to UIImage...


The other way round, there is no problem, I know


Kind Regards

DWD

convert cannot be the word. Maybe you can try to extract UIImage from Image, in the same way you build Image from UIImage.


It is not possible, see discussion here:

https://stackoverflow.com/questions/57028484/how-to-convert-a-image-to-uiimage

You know that it's easy to convert a UIImage to an Image and cannot find how to convert an Image? to an Binding of UIImage? .


Why don't you hold all images as UIImage ? You can convert it to Image, at any place you need Image.

import SwiftUI

class GetPicture: ObservableObject {//get picture from everywhere
    static let shared: GetPicture = GetPicture()
    
    @Published var image: UIImage? = nil
    
    func get() {
        image = UIImage(named: "ExamplePNG")
    }
}

struct WorkOnPicture: View {
    @Binding var image: UIImage?
    var body: some View {
        Text("Hello Picture")
    }
}

struct ContentView: View {
    @State var image: UIImage? = nil
    var body: some View {
        VStack {
            image.map{Image(uiImage: $0)}?.resizable().frame(width: 100, height: 100)
                .contextMenu {
                    Button(action: { self.workWithPicture() }) {
                        Text("Edit")
                        Image(systemName: "pencil")
                    }.disabled(image == nil)
            }
        }
        .onReceive(GetPicture.shared.$image) { image in
            self.image = image
        }
        .onAppear() {
            GetPicture.shared.get()
        }
    }
    func workWithPicture() {
        WorkOnPicture(image: $image) //<-
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


Generally, Image is a View and I do not recommend to hold Views in @State variables.

Hello Together,


thanks, to think after about that problem.


@OOPer: yep, this looks as the only solution for the long run.


Kind regards

DWD

Binding UIImage from Image
 
 
Q