如何拍摄所有子图层的截图

时间:2014-07-10 06:45:23

标签: ios screenshot

我可以使用代码获取ScreenShot。


    - (UIImage *)imageWithScreenByAllow
    {
    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    //support retina
    if(UIGraphicsBeginImageContextWithOptions != NULL)
    {
        UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size, NO, 0.0);
    } else {
        UIGraphicsBeginImageContext(screenWindow.frame.size);
    }
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
    }

问题是当我播放电影时(MPMoviePlayerController),ScreenShot上的电影区域是黑色但不是电影画面。

1 个答案:

答案 0 :(得分:0)

由于包含OpenGL,因此无法捕捉帧不断变化的画面。为此,您尝试并实现如下方法:

- (UIImage*) getGLScreenshot {
NSInteger myDataLength = 320 * 480 * 4;

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <480; y++)
{
    for(int x = 0; x <320 * 4; x++)
    {
        buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
    }
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];
return myImage;
}

- (void)saveGLScreenshotToPhotosAlbum {
UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil);  
}