问题
I was working on a game using SpriteKit
recently and found an interesting problem. When I initiated a SKSpriteNode
from an image saved in the Images.xcassets, it didn't work in some iOS 9
simulator. The code I used is:
let tortoise = `SKSpriteNode`(imageNamed: "tortoise")
When testing it in simulators, I found although it worked in most iOS 8.4 and iOS 9 simulators, it didn't work in iPad 2 (iOS 9) and iPhone 4s (iOS 9). The SKSpriteNode
wasn't displayed in those 2 simulators and the SKSpriteNode
size was (0.0, 0.0). Even when I manually set its SKTexture
using the same image source, I found the SKTexture
was loaded well (as it reported a correct size), but the SKSpriteNode
couldn't display the image.
Interestingly, when I tested the same above code in iPad 2 (iOS 8.4) and iPhone 4s (iOS 8.4) simulators, it worked fine. So I had to import some images for iPhone 4s and iPad 2 directly under the main bundle (not in the images assets) and use the code below to fix this problem:
if tortoise.size == CGSize(width: 0.0, height: 0.0) {
print("Reload tortoise image for SKSpiteNode")
tortoise = (isPad == true) ? SKSpriteNode(imageNamed: "tortoisePad.png") : SKSpriteNode(imageNamed: "tortoise@2x.png")
}
In this way, the SKSpriteNode
could be properly displayed in iPad 2 (iOS 9) and iPhone 4s (iOS 9) simulators. As I didn't have an iPad 2 or iPhone 4s device in hand, I couldn't test it in a real machine. I just wonder if this is a simulator bug or a change in iOS 9 for old devices (like iPad 2 and iPhone 4s)?
来源:https://stackoverflow.com/questions/33035819/skspritenode-cant-load-image-from-images-xcassets-in-some-ios-9-simulators