Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image.grayscale(1.0) returns a full color image in SwiftUI

Tags:

ios

swift

swiftui

I'm trying to convert an image to full grayscale with SwiftUI and am using .grayscale().

The documentation says the following for this method:

The intensity of grayscale to apply. Values closer to 0.0 are more colorful, and values closer to 1.0 are less colorful.

However, when I use .grayscale(1.0) a full-color image is returned but .grayscale(0.99) returns a full grayscale image.

Is this just a bug with SwiftUI or am I misunderstanding how this method works?

like image 573
Joe Scotto Avatar asked Oct 15 '19 19:10

Joe Scotto


People also ask

How do I change an image from grayscale to color?

Unfortunately this is not possible. Grayscale images do not contain sufficient information to create a color image. In instances where you see B&W/Grayscale images converted to color, this has been done manually in an application such as photoshop.


2 Answers

I'm guessing its a bug, but you can also use .saturation(0.0) and .saturation(1.0) to get the effect you're looking for.

like image 136
Peter Kovacs Avatar answered Oct 24 '22 03:10

Peter Kovacs


I was having the same error and I found this:

https://developer.apple.com/documentation/swiftui/text/grayscale(_:)

Seems like you have to use 0.9995 instead of 1 to achieve the gray behavior.

Gray Scale

like image 1
Gonzer Avatar answered Oct 24 '22 03:10

Gonzer