swiftswiftuiuiimageswiftui-image

Adjust UIImage Color


I'd like to modify the color of Images in SwiftUI. Currently, I'm trying to modify the .foregroundColor() as such, but it's not actually modifying it whatsoever from black

Image(uiImage: resizeImage(UIImage(systemName: isLiked ? "heart.fill" : "heart")!, targetSize: CGSize(width: 75, height: 27))!)
    .foregroundColor(isLiked ? .yellow : .gray)

As you can see, the color is still black:

enter image description here

Ideally, I'd want to modify it to have this .foregroundColor, that is the color of its outline, and when the like button (heart) is pressed, it fills with that same color: foregroundColor(colorPalettes[safe: sharedVars.colorPaletteIndex]?[2] ?? .white)

This is currently the color being applied to the like count next to the like button (heart image):

enter image description here


Solution

  • Credit to @aheze and @LawrenceGimenez for their comments under my question suggesting these changes:

    Image(systemName: isLiked ? "heart.fill" : "heart")
        .font(.title)
        .scaleEffect(1)
        .foregroundStyle(colorPalettes[safe: sharedVars.colorPaletteIndex]?[2] ?? .white)
    

    Using the base Image object and adjusting its .foregroundStyle works as intended in my question for getting the color to match my variable:

    enter image description here

    enter image description here