问题
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