1

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()
        }
    }
}
11
  • .cornerRadius(10) .clipped() ? Commented Apr 4, 2021 at 21:00
  • It's working for me. Can you show what code is surrounding this?
    – aheze
    Commented Apr 4, 2021 at 21:02
  • I believe the issue is that I am loading the Image from a UI Image. Does it work for you when you do that? Commented Apr 4, 2021 at 21:04
  • 1
    @SauravKumar works fine... screenshot. We probably need some more code.
    – aheze
    Commented Apr 4, 2021 at 21:16
  • 1
    That's weird, I suppose the issue is with my UIImage, right? I just updated the post with the full view Commented Apr 4, 2021 at 21:18

1 Answer 1

2

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)
            
        }
 
    }
}
9
  • Not sure what the problem is then – the image shows up, it's corners just aren't rounded Commented Apr 4, 2021 at 21:31
  • have you tried my given codes? my Answer works 100%, you changed your question several time you started from question about cornerRadius and ended with debugging your project! please be more clear and more focus when you are asking for help.
    – swiftPunk
    Commented Apr 4, 2021 at 21:38
  • Yep, that doesn't seem to work either – not sure what the problem is Commented Apr 4, 2021 at 21:39
  • The only problem is cornerRadius – not trying to debug anything Commented Apr 4, 2021 at 21:40
  • 1
    @SauravKumar try to create a minimum reproducible example.
    – aheze
    Commented Apr 4, 2021 at 21:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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