0

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

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

4

1 回答 1

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];
}
于 2011-04-01T14:05:45.537 回答