iOS 捕捉“截图"相机控制器 [英] iOS Capture "screenshot" of camera controller

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

问题描述

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

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!!!

推荐答案

它并没有比 Apple 在 如何捕捉相机的视野.是的,这确实涉及到类 AVCaptureSession.

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.

如果你真的需要界面截图,你应该去相关文档.从链接中剪切和粘贴代码(如果这不起作用,您应该向 Apple 提交错误报告):

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):

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

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天全站免登陆