Sparrow-Framework:屏幕截图SPView(iOS)

时间:2011-02-01 17:30:28

标签: iphone ios screen-capture sparrow-framework

我在另一个StackOverflow问题上找到了此链接:http://reusablesnippets.posterous.com/capture-uiview

详细介绍如何使用UIGraphicsBeginImageContext()捕获屏幕内容。我在一个Sparrow框架项目中,我的SPView只返回一个黑色矩形。是否无法捕获SPView(UIView的子类)?

1 个答案:

答案 0 :(得分:0)

我不知道你是否在其他地方得到了答案,但我刚才写了一个小方法来做这件事。 http://iky1e.tumblr.com/post/3054935938/save-opengl-content-to-photolibrary

-(void)saveRectangle:(SPRectangle*)rectangle{
        int bufferLenght = (rectangle.width*(rectangle.height+30)*4);

        int myWidth = rectangle.width;
        int myHeight = rectangle.height;
        int myY = self.stage.height-rectangle.y-rectangle.height;
        int myX = rectangle.x;

        unsigned char buffer[bufferLenght];
        glReadPixels(myX, myY, myWidth, myHeight, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);

        CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, bufferLenght, NULL);
        CGImageRef iref = CGImageCreate(myWidth,myHeight,8,32,myWidth*4,CGColorSpaceCreateDeviceRGB(),
                                                                        kCGBitmapByteOrderDefault,ref,NULL, true, kCGRenderingIntentDefault);
        uint32_t* pixels = (uint32_t *)malloc(bufferLenght);
        CGContextRef context = CGBitmapContextCreate(pixels, myWidth, myHeight, 8, myWidth*4, CGImageGetColorSpace(iref),
                                                                                                 kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
        CGContextTranslateCTM(context, 0.0, myHeight);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, CGRectMake(0.0, 0.0, myWidth, myHeight), iref);
        CGImageRef outputRef = CGBitmapContextCreateImage(context);
        UIImage *image = [[UIImage alloc] initWithCGImage:outputRef];
        free(pixels);

        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        [image release];
}