2012-12-06 35 views
2

我正在開發iOS6中的iPad應用程序,該應用程序顯示用不同顏色和紋理設計房屋。爲此,我正在使用cocos2d。爲了顯示家中使用的紋理和顏色,我使用了UIKit視圖。 現在我想截取包含cocos2d圖層和UIKit視圖的這個視圖的截圖。 如果我使用cocos2d的喜歡拍攝畫面截圖:如何捕捉包含cocs2d和UIKit的視圖的屏幕截圖

UIImage *screenshot = [AppDelegate screenshotWithStartNode:n]; 

則僅取的cocos2d層的一個單元。

否則,如果我使用的拍攝畫面拍攝的UIKit喜歡:

UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 

則僅取的UIKit組件的捕獲和停電的cocos2d的一部分。

我想他們都在同一個屏幕截圖...

回答

0

我研究了很多關於這個......而且目前我找不到任何代碼可以將包含cocos2d和UIKit的屏幕截屏在一起。即使有一些可用的代碼,但它不適用於AppStore。所以,如果你使用該代碼,你的應用程序將被從AppStore拒絕。

所以現在,我找到了一個臨時解決方案,以實現這一目標:

首先,我把我的cocos2d層的截圖,然後把那個截圖的UIImageView並補充說UIImageView我的背後屏幕上的所有本UIViews這樣,使得用戶不能想像這個事件:

[self.view insertSubview:imgView atIndex:1]; 

在指數1,因爲我的cocos2d層是如此高於指數0 ...現在

,即cocos2d圖片是我的UIKit視圖的一部分,我用正常的UIKit方式截取了當前屏幕的屏幕截圖。我們在那裏。我現在有包含兩個視圖的屏幕截圖。

這對我來說現在工作。如果有人找到有效的解決方案,那麼最歡迎! 我會等待任何可行的解決方案。 謝謝大家的幫助!

0

試試這個方法,只是改變一些代碼,使您的要求..

-(UIImage*) screenshotUIImage 
{ 
    CGSize displaySize = [self displaySize]; 
    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); 

    switch (deviceOrientation_) 
    { 
     case CCDeviceOrientationPortrait: break; 
     case CCDeviceOrientationPortraitUpsideDown: 
      CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(180)); 
      CGContextTranslateCTM(context, -displaySize.width, -displaySize.height); 
      break; 
     case CCDeviceOrientationLandscapeLeft: 
      CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(-90)); 
      CGContextTranslateCTM(context, -displaySize.height, 0); 
      break; 
     case CCDeviceOrientationLandscapeRight: 
      CGContextRotateCTM(context, CC_DEGREES_TO_RADIANS(90)); 
      CGContextTranslateCTM(context, displaySize.width * 0.5f, -displaySize.height); 
      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; 
} 

- (Texture2D*) screenshotTexture { 
    return [[Texture2D alloc] initWithImage:[self screenshotUIImage]]; 
} 

獲取更多信息請參見This Link

參考所有回答和評論其非常有趣

我希望這可以幫到你

+0

沒有親愛的...它只需要cocos2d圖層的屏幕截圖... UIKit組件不在拍攝... –

+0

哦然後嘗試給定的鏈接我發現任何東西然後將張貼 –

+0

@KananVora試試這個也哥們..見闕也可能你從這個想法得到一些想法.. http://stackoverflow.com/questions/962390/capturing-eaglview-content-with-alpha-channel-on-iphone –