WKImage always return nil

寵の児 提交于 2020-01-06 03:14:29

问题


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.

Update: Updated screen shot


回答1:


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



来源:https://stackoverflow.com/questions/37984502/wkimage-always-return-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!