2

I do not understand why the car systemName image does not appear on Apple Watch Simulator. (it is black)

import SwiftUI
struct ContentView: View {
    var body: some View {
        Image(uiImage: UIImage(systemName: "car")!)
    }
}

struct preview : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Click here to see how it looks (the screen is completely black, like if nothing was added to the view)

2 Answers2

3

I suppose it should be

enter image description here

struct ContentView: View {
    var body: some View {
        Image(systemName: "car") // < Native SwiftUI
    }
}

or... if you so like UIImage, the same result with

Image(uiImage: UIImage(systemName: "car")!).colorInvert()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
0

A solution would be to set the UIImage color to white, like that it will be visible

Image(uiImage: UIImage(systemName: "car")!.maskWithColor(color: .white)!)

where maskWithColor comes from https://stackoverflow.com/a/36591030/12893419

extension UIImage {

    func maskWithColor(color: UIColor) -> UIImage? {
        let maskImage = cgImage!

        let width = size.width
        let height = size.height
        let bounds = CGRect(x: 0, y: 0, width: width, height: height)

        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
        let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)!

        context.clip(to: bounds, mask: maskImage)
        context.setFillColor(color.cgColor)
        context.fill(bounds)

        if let cgImage = context.makeImage() {
            let coloredImage = UIImage(cgImage: cgImage)
            return coloredImage
        } else {
            return nil
        }
    }

}
LetsGoBrandon
  • 498
  • 8
  • 23