简体   繁体   English

如何在iPhone中使用相机预览拍摄屏幕截图?

[英]How to take screenshot with camera preview in iPhone?

i can take screenshots now but in the place of camera preview, It appears a black display and the camera preview is not been displayed. 我现在可以截图,但可以代替相机预览,它显示为黑色,并且不显示相机预览。 Any ideas ? 有任何想法吗 ?

Here my code is : 这是我的代码:

CGRect screenRect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(screenRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor whiteColor] set];
CGContextFillRect(ctx, screenRect);
[self.view.layer renderInContext:ctx];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return img;

Now I m currently use this code. 现在,我目前正在使用此代码。

Your code seems to be correct doesn't figure out what is going wrong in your code.I suggest you to try out this code.It works for me. 您的代码似乎是正确的,无法弄清楚代码中出了什么问题。我建议您尝试一下此代码,它对我有用。

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

courtesy:- https://stackoverflow.com/a/8996637/1865424 礼貌:-https: //stackoverflow.com/a/8996637/1865424

you can read this thread to know more about Screenshot capture. 您可以阅读此主题以了解有关屏幕截图捕获的更多信息。 https://discussions.apple.com/message/8358740?messageID=8358740#8358740?messageID=8358740 https://discussions.apple.com/message/8358740?messageID=8358740#8358740?messageID=8358740

Hope this will help you out. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM