gpt4 book ai didi

iphone - 我在线程中加载的图像没有出现

转载 作者:行者123 更新时间:2023-12-03 20:07:51 24 4
gpt4 key购买 nike

我正在使用 CCScrollLayer,我想在玩家选择阶段时准备图像。这很简单,但是当我滚动阶段时,它会占用时间(延迟加载图像)。

所以我决定使用NSThread。我从 cocos2d 收到一条消息“cocos2d:CCSpriteFrameCache:尝试使用文件'Level3.png'作为纹理”。那么它应该会出现。但我在线程上加载的这些图像没有按我想要的方式显示。只是什么都没有。

-(void) moveToStagePage:(int)page
{
...
[NSThread detachNewThreadSelector:@selector(prepareTexture:) toTarget:self withObject:[NSNumber numberWithInt:page]];
...
}

下面的源代码是准备图像的代码。(线程)

-(void) prepareTexture:(NSNumber*)number
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

int _page = [number intValue];
NSLog(@"%d Thread start", _page);

if(loadingTexNum != 0 && (_page + 1) != loadingTexNum)
{
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:[NSString stringWithFormat:@"Level%d.plist", loadingTexNum]];
loadingTexNum = _page + 1;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"Level%d.plist", loadingTexNum]];
}

if(loadingTexNum == 0 && (_page + 1) != loadingTexNum)
{
loadingTexNum = _page + 1;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"Level%d.plist", loadingTexNum]];
}

[NSThread sleepForTimeInterval:10.0];
NSLog(@"%d Thread release", _page);
[pool release];
}

最佳答案

OpenGL 不支持在多个线程上加载,除非您传递 openGL 上下文。

Each thread in a Mac OS X process has a single current OpenGL rendering context. Every time your application calls an OpenGL function, OpenGL implicitly looks up the context associated with the current thread and modifies the state or objects associated with that context.

OpenGL is not reentrant. If you modify the same context from multiple threads simultaneously, the results are unpredictable. Your application might crash or it might render improperly. If for some reason you decide to set more than one thread to target the same context, then you must synchronize threads by placing a mutex around all OpenGL calls to the context, such as gl* and CGL*. OpenGL commands that block—such as fence commands—do not synchronize threads.

Source

您可以在 CCTextureCache 中使用 - (void) addImageAsync:(NSString *)filename target:(id) targetselector:(SEL)selector;类,在主线程上调用它。

或者您可以实现自己的,实际上与 addImageAsync 相同。

供您引用,这是 CCTextureCache 实现加载图像的方式:

 NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];

// textures will be created on the main OpenGL context
// it seems that in SDK 2.2.x there can't be 2 threads creating textures at the same time
// the lock is used for this purpose: issue #472
[contextLock lock];
if( auxEAGLcontext == nil ) {
auxEAGLcontext = [[EAGLContext alloc]
initWithAPI:kEAGLRenderingAPIOpenGLES1
sharegroup:[[[[CCDirector sharedDirector] openGLView] context] sharegroup]];

if( ! auxEAGLcontext )
CCLOG(@"cocos2d: TextureCache: Could not create EAGL context");
}

if( [EAGLContext setCurrentContext:auxEAGLcontext] ) {

// load / create the texture
CCTexture2D *tex = [self addImage:async.data];

// The callback will be executed on the main thread
[async.target performSelectorOnMainThread:async.selector withObject:tex waitUntilDone:NO];

[EAGLContext setCurrentContext:nil];
} else {
CCLOG(@"cocos2d: TetureCache: EAGLContext error");
}
[contextLock unlock];

[autoreleasepool release];

关于iphone - 我在线程中加载的图像没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7959896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com