iOS捕获“屏幕截图”的摄像机控制器 [英] iOS Capture "screenshot" of camera controller

查看:215
本文介绍了iOS捕获“屏幕截图”的摄像机控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我显示的相机,我正在使用UIGetScreenImage屏幕截图,(我试过UIGraphicsGetImageFromCurrentImageContext,它的工作伟大的屏幕截图几乎在我的应用程序的任何部分,但对于相机视图,它将只返回一个空白的白色图像)...反正,我害怕苹果会拒绝我的应用程序,因为UIGetScreenImage ...如何从相机的左上角的一个50×50像素的屏幕截图,而不使用这种方法?我搜索,所有我可以找到的是AVCaptureSession,我找不到很多的是什么,或者如果它甚至我正在寻找...任何洞察力? :) //developer.apple.com/library/ios/#qa/qa1702/_index.htmlrel =nofollow>如何捕获相机的视图。是的,这确实涉及到 AVCaptureSession



如果你确实需要截图,您应该参阅文档。从链接剪切并粘贴代码(如果这不工作,您应该向Apple提交错误报告):



更新:似乎在较新版本的iOS上不再支持此方法。第二个链接现在也被破坏了。

   - (UIImage *)截图
{
//使用目标大小创建图形上下文
//在iOS 4及更高版本上,使用UIGraphicsBeginImageContextWithOptions来考虑比例
//在iOS 4之前,回退到使用UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds] .size;
if(NULL!= UIGraphicsBeginImageContextWithOptions)
UIGraphicsBeginImageContextWithOptions(imageSize,NO,0);
else
UIGraphicsBeginImageContext(imageSize);

CGContextRef context = UIGraphicsGetCurrentContext();

//从后面到前面迭代每个窗口
for([UIApplication sharedApplication] windows中的UIWindow *窗口])
{
if(!
{
// -renderInContext:在图层的坐标空间中渲染,
//所以我们必须首先将图层的几何图形应用到图形上下文
CGContextSaveGState(context);
//将上下文定位在窗口的定位点
CGContextTranslateCTM(context,[window center] .x,[window center] .y);
//应用关于锚点的窗口变换
CGContextConcatCTM(context,[window transform]);
//由锚点左侧和上方的边界部分偏移
CGContextTranslateCTM(context,
- [window bounds] .size.width * [[window layer] anchorPoint]。 x,
- [window bounds] .size.height * [[window layer] anchorPoint] .y);

//将层层次结构渲染到当前上下文
[[window layer] renderInContext:context];

//恢复上下文
CGContextRestoreGState(context);
}
}

//检索屏幕截图
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;
}


In my app I display the camera and I am taking screenshots of certain perts using UIGetScreenImage, (I tried UIGraphicsGetImageFromCurrentImageContext and it works great for screenshots on almost any part of my app but for the camera view it will just return a blank white image) ... Anyways, I fear Apple will reject my app because of UIGetScreenImage... How can I take a "screenshot" of a 50px by 50px box from the upper left corner of the camera without using this method? I searched and all I could find was "AVCaptureSession" and I couldn't find much about what that does, or if it's even what I'm looking for... Any insight? :) Thanks guys!!!

解决方案

It doesn't get much clearer than Apple's docs on how to capture the camera's view. Yes, this does involve the class AVCaptureSession.

If you actually need a screenshot of the interface, you should take at the docs for that. Cut-and-paste code from the link (if this does not work, you should submit a bug report to Apple):

Update: It appears that this approach is no longer supported on newer versions of iOS. The second link is now broken as well.

- (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;
}

这篇关于iOS捕获“屏幕截图”的摄像机控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆