2

在我的应用程序中,我显示相机并使用 UIGetScreenImage 截取某些 perts 的屏幕截图(我尝试了 UIGraphicsGetImageFromCurrentImageContext,它适用于我的应用程序几乎任何部分的屏幕截图,但对于相机视图,它只会返回一个空白的白色图像)。 .. 无论如何,我担心 Apple 会因为 UIGetScreenImage 拒绝我的应用程序...如何在不使用此方法的情况下从相机左上角拍摄 50 像素 x 50 像素框的“屏幕截图”?我搜索了一下,我能找到的只有“AVCaptureSession”,但我找不到太多关于它的作用,或者它是否是我正在寻找的......有什么见解吗?:) 多谢你们!!!

4

2 回答 2

3

它并没有比 Apple 的关于如何捕捉相机视图的文档更清楚。是的,这确实涉及到类AVCaptureSession

如果您确实需要界面的屏幕截图,您应该查看文档。从链接中剪切和粘贴代码(如果这不起作用,您应该向 Apple 提交错误报告):

更新:新版本的 iOS 似乎不再支持这种方法。第二个链接现在也断开了。

- (UIImage*)screenshot 
{
    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) 
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer's geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window's anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window's transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}
于 2012-01-25T01:05:00.817 回答
0

从 iOS7 开始,您可以使用:

drawViewHierarchyInRect

UIImage *image;
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:YES];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
于 2014-11-16T13:42:49.103 回答