Anand Suthar1987 Anand Suthar1987 - 5 months ago 13
Swift Question

WKImage always return nil

Access assets image will always return nil, below is my code and assets catalog screen shot.

let image = WKImage(imageName: "sample")
print(image.image)


This will always prints nil.

enter image description here

Update: Updated screen shot

Answer

I found that from your screenshot you are setting the Image Set in Assests.xcassets in the swiftWatch WatchOS target and i think you are use that image for the WKDemo target so you get that nil.

Set that Image Set in your WKDemo's Assests.xcassets instead of WatchOS's Assests.xcassets then check.

That issue is your are setting ImageSet in different Target and your are try to load in Different Target.

UPDATE

After checking your sample project you are doing wrong code for getting image. Instead of let image = WKImage(imageName: "sample") you must be use WKPickerItem() object like following code:

 for i in 1...10 {
            let item = WKPickerItem()
            item.title = "Picker itme =\(i)"
            item.contentImage = WKImage(imageName: "sample")

            if let image = item.contentImage
            {
             print(image)
            }
            pickerItems.append(item)
        }

OUTPUT IS

enter image description here