0

I am using an ImagePicker to select an Image from Library and store in CoreData (as Binary). In another view this selected image is shown as a thumbnail.

I want the user to be free when selecting an image. If he doesn't select an image, it should show a default Thumbnail (which is already shown in the placeholder).

My question is whether the Code below is right logically but not well implemented.

ZStack (alignment: .bottomLeading) {

                                Image(uiImage: UIImage(data: Group.groupThumbnail ?? self.image)!)
                                    .resizable(capInsets: EdgeInsets())
                                    .aspectRatio(contentMode: .fill)
                                    .frame(height: 200.0, alignment: .center)
                                    .cornerRadius(22)

My Idea would be to do it this way, but it says that this does not work.

ZStack (alignment: .bottomLeading) {

    Image(uiImage: UIImage(data: Group.groupThumbnail ?? self.image) ?? Image("defaultGroupThumbnail"))
        .resizable(capInsets: EdgeInsets())
        .aspectRatio(contentMode: .fill)
        .frame(height: 200.0, alignment: .center)
        .cornerRadius(22)

I appreciate any advice. Thanks in Advance!

1 Answer 1

1
 Image(uiImage: UIImage(data: Group.groupThumbnail ?? self.image) ?? Image("defaultGroupThumbnail"))

This is providing a default value for UIImage using an Image, hence the type mismatch. Replace it with:

 Image(uiImage: UIImage(data: Group.groupThumbnail ?? self.image) ?? UIImage(named: "defaultGroupThumbnail"))
2
  • Thanks for the answer. It worked. But I had to add a "!". Like following: // Image(uiImage: (UIImage(data: Group.groupThumbnail ?? self.image) ?? UIImage(named: "defaultGroupThumbnail"))!)
    – Drew
    Jan 31, 2022 at 15:16
  • No problem as long as the image is in your assets folder
    – Timmy
    Feb 1, 2022 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.