Saurav Kumar
Saurav Kumar

Reputation: 39

SwiftUI .cornerRadius on Image not showing up

I am currently working on a SwiftUI Chat View project, and I am trying to round the corner of an image, which is loaded via a URL and converted from UIImage to Image. Any ideas why .cornerRadius(10) isn't working?

Image(uiImage: imageFromFirebase!)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: UIScreen.main.bounds.width / 1.5, alignment: isCurrentUser ? .trailing : .leading)
                .clipped()
                .cornerRadius(10)

Here is the full container – there is probably a lot of unnecessary stuff here but in case it helps:

struct ContentMessageView: View {
    var contentMessage: String
    var isCurrentUser: Bool
    var type: String
    var name: String
    var color: Color
    var image: UIImage?
    @State private var imageFromFirebase: UIImage?
    
    func imageLoadFromURL(){
        HTTPRequest.fetchImage(self.contentMessage) { (image) in
            if image != nil {
                imageFromFirebase = image!
            }
        }
    }
    
    var body: some View {
        VStack{
            if type == "image" {
                
                if !isCurrentUser {
                    HStack{
                        Text(name).font(.caption).foregroundColor(Color.black.opacity(0.2)).padding(.horizontal)
                        Spacer()
                    }
                }
                HStack{
                    if isCurrentUser {
                        Spacer()
                    }
                    if imageFromFirebase != nil {
                        ZStack{
                            Image(uiImage: imageFromFirebase!)
                                .resizable()
                                .scaledToFit()
                                .cornerRadius(10)
                                .frame(width: UIScreen.main.bounds.width / 1.5, alignment: isCurrentUser ? .trailing : .leading)
//                                .overlay(RoundedRectangle(cornerRadius: 10)
                                .background(Color.black)
//                                    .stroke(isCurrentUser ? Color(UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0)) : color, lineWidth: 5))
                        }
                        .shadow(color: Color.black.opacity(0.05), radius: 10)
                    }
                    if !isCurrentUser {
                        Spacer()
                    }
                }.padding(.horizontal, 10)
            }
        }.onAppear(){
            self.imageLoadFromURL()
        }
    }
}

Upvotes: 1

Views: 2127

Answers (1)

swiftPunk
swiftPunk

Reputation: 1

It is working fine, you did use wrong syntax and placement for code!

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        
        if let unwrappedUIImage: UIImage = UIImage(named: "your image name here") {
            
            Image(uiImage: unwrappedUIImage)
                .resizable()
                .scaledToFit()
                .cornerRadius(30)
                .frame(width: 300, height: 300, alignment: .center)
            
        }
 
    }
}

Upvotes: 2

Related Questions