首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当相机在-iOS上时拍摄截图

当相机在-iOS上时拍摄截图
EN

Stack Overflow用户
提问于 2013-09-05 21:57:52
回答 1查看 1.7K关注 0票数 3

我正在开发一个AR应用程序,在我的项目中使用苹果的AVCam示例代码。我试图用下面的方法拍摄截图,这些方法我在这里已经找到了,但它们并不适用于我。有什么建议吗?谢谢。

代码语言:javascript
复制
    - (UIImage *) screenshot {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;

        if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
            UIGraphicsBeginImageContextWithOptions(window.bounds.size, NO, [UIScreen mainScreen].scale);
        else
            UIGraphicsBeginImageContext(window.bounds.size);
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSData * data = UIImageJPEGRepresentation(image, 1.0);

        return [UIImage imageWithData:data];
    }

    - (UI

Image*)screenshot2
{
    // 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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-06 00:42:29

代码不工作,因为CALayer的renderInContext不呈现来自摄像机的内容。

请参阅以下参考资料:index.html

正如上面的链接所描述的,您可以:

1)使用相关的AVFoundation API (ref/doc/uid/DTS40010192)捕获摄像机的内容

2)然后将捕获的图像绘制到呈现UIKit元素的图形上下文中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18646632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档