Reputation: 18729
Is it possible to use a views .mask(...)
modifier together with a grayscale image. I mean just like one uses a layer mask in Photoshop.
While it is not problem to use an Image
as mask view, this results in a simple rectangle mask with the size of the image. The image content is not considered for masking.
I would like use an image as mask to apply a complex opacity mask, e.g. like the cloud in this image
Upvotes: 0
Views: 437
Reputation: 30391
The key here is to convert the image to use alpha depending on luminance - a perfect use for the luminanceToAlpha()
modifier.
Result:
Code:
struct ContentView: View {
var body: some View {
Text("This is a red view with some text") // 1
.padding(50)
.background(Color.red)
.mask { // 2
Image("cloud") // 3
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 350)
.luminanceToAlpha() // 4
.fixedSize() // 5
}
}
}
How it works:
Upvotes: 3