YogiBear
YogiBear

Reputation: 51

How to show UIImage using SwiftUI?

I'm using a picker to grab an image from the phone gallery. It returns a UIImage which I want to show a thumbnail view of but I can't seem to convert it correctly?

truncated code:

struct ContentView: View {

@State var image: UIImage? = nil
@State var thumbImage: Image? = nil
@State var showImagePicker: Bool = false
@State private var selectedImage = ""


var body: some View {
    NavigationView {
         VStack {
                thumbImage = Image(UIImage: self.image)
                thumbImage!.resizable()
                    .frame(width: 299, height: 299)
                    .shadow(radius: 10)

                if (showImagePicker) {
                    OpenGallery(isShown: $showImagePicker, image: $image)
                }
        }
    }
    .navigationBarTitle("Test")
    }
}

It just doesn't like the way I've used Image? navigationbartitle doesn't display either but not as big a problem for me atm.

Upvotes: 2

Views: 7607

Answers (1)

Chris
Chris

Reputation: 8126

it would be nice if you would try out your "truncated" code first, because it was not compilable at all.

I had to change some things, but now it "runs" if you have the image in our Assets.

Here is my corrected code:

But I also have to say I am not sure what you exactly want to achieve....

struct ContentView: View {

@State var image: UIImage? = UIImage(named: "80")
@State var thumbImage: Image? = nil
@State var showImagePicker: Bool = false
@State private var selectedImage = ""


    var body: some View {
        NavigationView {
            VStack {
                Text("aha")
                Image(uiImage: self.image!).resizable()
                    .frame(width: 299, height: 299)
                    .shadow(radius: 10)


            }
        }
        .navigationBarTitle("Test")
    }
}

Upvotes: 2

Related Questions