我在使用以下代码时遇到问题,该代码通过调用 SKTexture 的大小来加载它。代码在调用 size 的行每 100 次运行中大约有 1 次随机崩溃。还有其他一些用于预加载图像的 SKTexture 方法,但它们也会更频繁地导致崩溃!我使用 TextureAtlases 来防止崩溃,我的所有 SKTexure 加载都是通过这种方法发生的。
/* add image to dictionary thread-safe */
-(SKTexture*) getThreadSafeDictionaryContainImageOtherwiseLoadAndReturn:(NSString*) imageToLoad andForceLoad:(BOOL) forceLoad{
// to control the enviroment where the image dictionary is modified, go ahead and lock it down with an NSLock
[self.dictionaryModificationLock lock];
SKTexture *toReturn = nil;
CGSize size = CGSizeZero;
// first, ignore duplicate loads by looking for the image in an NSDictionary called "allImages"
if ((toReturn = [self.allImages objectForKey:imageToLoad])){
// have the image to return already loaded
}else{
// grab the SKTexture and force it to load by requesting it's size
SKTexture *texture = [SKTexture textureWithImageNamed:imageToLoad];
if (forceLoad)
size = texture.size; // crashes here!
[self.allImages setObject:texture forKey:imageToLoad];
toReturn = texture;
}
[self.dictionaryModificationLock unlock]; // unlock the NSLock
return toReturn;
}
崩溃报告如下所示:
Fatal Exception: NSGenericException
*** Collection <NSConcreteMapTable: 0x1c059ff0> was mutated while being enumerated.
0 CoreFoundation __exceptionPreprocess + 126
2 CoreFoundation -[NSException name]
3 Foundation -[NSConcreteMapTable countByEnumeratingWithState:objects:count:] + 56
4 CoreFoundation -[__NSFastEnumerationEnumerator nextObject] + 110
5 SpriteKit +[SKTextureAtlas(Internal) findTextureNamed:] + 284
6 SpriteKit __26-[SKTexture loadImageData]_block_invoke + 1654
7 SpriteKit SKSpinLockSync(int*, void () block_pointer) + 104
8 SpriteKit -[SKTexture loadImageData] + 302
9 SpriteKit -[SKTexture size] + 42
我发现其他一些线程说这是 SpriteKit 的问题。但我认为必须有一些解决方法。你们有什么感想?
谢谢,
加伦