Shark Deng
Shark Deng

Reputation: 1058

SwiftUI transparent png becomes white (jpg)

I have a png which is partically transparent, namely it has alpha channel. But when I used the following code, the transparent becomes white. I guess the alpha channel is missing. Do you know how to solve this problem? Thank you.

This is the reproducible code.

Image("power")
.resizable()
.aspectRatio(contentMode: .fit)

This is the image

enter image description here

This is the effect on simulator

enter image description here

Upvotes: 3

Views: 3224

Answers (2)

Shark Deng
Shark Deng

Reputation: 1058

I used this code to manage to show the transparent effect, but foreground colors are lost.

Color.black
    .frame(width: home.screenWidth-2*home.defaultPadding,
    height: home.screenWidth)
    .mask(Image("power")
      .resizable()
      .aspectRatio(contentMode: .fit))

Reason

Thank you for your helps so much!!!! Without your support, I cann't find the reason. enter image description here

The compression for the png should be automatic, my project is inherited(basic) somehow, that's why it rendered white.

The code and image has no problem.

Upvotes: 1

CALL-151
CALL-151

Reputation: 564

That's what should happen. You've got an image with an alpha channel over a white background, so you can't see any of the white elements in your image. Change your Color Scheme to dark with .preferredColorScheme(.dark) and you'll see this:

enter image description here

Upvotes: 0

Related Questions