我有一个基于图层的类,其中包含两个精灵作为子级。让我们称呼它们:背景和树。
在这个类中,我有一个方法来捕获该层的屏幕截图。该方法工作正常。每次我拍摄该图层的截图时,我都会在背景中获得树的构图。精细。在某个时刻,我想获得没有树的那个图层的截图。所以我要做的是,隐藏树,截取屏幕截图并再次显示树...就像这样:
[myLayer hideTree];
UIImage *screenshot = [myLayer screenshot];
[myLayer showTree];
令我惊讶的是,以这种方式生成的屏幕截图始终包含树。
这是hideTree和showTree:
- (void) hideTree {
[treeLayer setOpacity:0];
// I have also tried [treelayer setVisible:NO];
}
- (void) showTree {
[treeLayer setOpacity:255];
// I have also tried [treelayer setVisible:YES];
}
我正在使用此方法进行截屏,来自cocos2d论坛:
-(UIImage*) screenshot
{
CGSize displaySize = [[CCDirector sharedDirector] winSize];
CGSize winSize = [self winSize];
//Create buffer for pixels
GLuint bufferLength = displaySize.width * displaySize.height * 4;
GLubyte* buffer = (GLubyte*)malloc(bufferLength);
//Read Pixels from OpenGL
glReadPixels(0, 0, displaySize.width, displaySize.height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
//Make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
//Configure image
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * displaySize.width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(displaySize.width, displaySize.height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);
CGContextRef context = CGBitmapContextCreate(pixels, winSize.width, winSize.height, 8, winSize.width * 4, CGImageGetColorSpace(iref), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextTranslateCTM(context, 0, displaySize.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation)
{
case UIDeviceOrientationPortrait: break;
case UIDeviceOrientationPortraitUpsideDown:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180));
CGContextTranslateCTM(context, -displaySize.width, -displaySize.height);
break;
case UIDeviceOrientationLandscapeLeft:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90));
CGContextTranslateCTM(context, -displaySize.height, 0);
break;
case UIDeviceOrientationLandscapeRight:
CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90));
CGContextTranslateCTM(context, displaySize.width * 0.5f, -displaySize.height);
break;
case UIDeviceOrientationUnknown:
break;
case UIDeviceOrientationFaceUp:
break;
case UIDeviceOrientationFaceDown:
break;
}
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, displaySize.width, displaySize.height), iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
UIImage *outputImage = [UIImage imageWithCGImage:imageRef];
//Dealloc
CGImageRelease(imageRef);
CGDataProviderRelease(provider);
CGImageRelease(iref);
CGColorSpaceRelease(colorSpaceRef);
CGContextRelease(context);
free(buffer);
free(pixels);
return outputImage;
}
我错过了什么?树为什么总是显示?
答案 0 :(得分:2)
您的代码在游戏循环的更新过程中正在运行。
截图是获取用于创建UIImage的当前像素。
当您告诉树不可见或不透明度为零时,直到下一个绘制周期才会生效。
要解决您的问题,您需要隐藏树,执行绘制周期,然后在下次更新中捕获未显示树的帧。
有意义吗?
答案 1 :(得分:1)
当您更改树的不透明度时,它将仅在下次屏幕更新时生效。
一旦我遇到这个问题并且我做了一些时间表。而不是
[myLayer hideTree];
UIImage *screenshot = [myLayer screenshot];
[myLayer showTree];
待办事项
[myLayer hideTree];
[self schedule:@selector(takeScreenshot)interval:0.05];
并在场景中添加以下方法:
-(void)takeScreenshot {
[self unschedule:@selector(takeScreenshot)];
UIImage *screenshot = [myLayer screenshot];
[myLayer showTree];
}
基本上,我们将0.05秒的间隙用于更新屏幕。这应该有用。
上面的方法对我有用,但我认为如果你在隐藏树之后调用[self visit]
它也有效。但我从未测试过。