是否可以拍摄包括相机在内的组合视图的快照? [英] Is it possible to take a snapshot of combined views including the camera?

查看:79
本文介绍了是否可以拍摄包括相机在内的组合视图的快照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有一个按钮,该按钮应通过调用"takeSnapShot"来获取屏幕上所有内容的屏幕截图(请参见下面的代码).我有两个视图,其中一个是我用于相机的选择器视图,因此在屏幕上,我看到了来自相机的图像,在它旁边是另一个包含图像的视图.

My app has a button that should take a screenshot of whatever is on the screen by calling "takeSnapShot" (see code below). I have two views and one of them is a picker view that I use for the camera, so in the screen I see the image coming from the camera and next to it another view with images.

问题是我捕获了屏幕,但没有捕获到来自相机的图像.

The thing is that I capture the screen but it doesn't capture the image coming from the camera.

另外,我想我渲染了view.layer,但是调试器一直在说>

Also, I think I render the view.layer but the debugger keeps saying>

快照未渲染的视图将导致空白快照.确保您的视图至少被渲染过一次快照或屏幕更新后的快照."

"Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates."

有什么想法吗?谢谢!

这是代码:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

UIImage *capturaPantalla;


-(void)takeSnapShot:(id)sender{

   UIGraphicsBeginImageContext(picker.view.window.bounds.size);
   [picker.view.layer renderInContext:UIGraphicsGetCurrentContext()];
   capturaPantalla = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   UIImageWriteToSavedPhotosAlbum(capturaPantalla,nil,nil,nil);

}

推荐答案

我以前做过类似的功能,并使用AVCaptureSession来实现.

I did the similar function before, and used AVCaptureSession to make it.

 // 1. Create the AVCaptureSession
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    [self setSession:session];

 // 2. Setup the preview view
    [[self previewView] setSession:session];

 // 3. Check for device authorization
    [self checkDeviceAuthorizationStatus];

    dispatch_queue_t sessionQueue = dispatch_queue_create("session queue", DISPATCH_QUEUE_SERIAL);
    [self setSessionQueue:sessionQueue];

============================================我的倒数快照功能:

============================================ My countdown snapshot function:

- (void) takePhoto
{
    [timer1 invalidate];
    dispatch_async([self sessionQueue], ^{
        // 4. Update the orientation on the still image output video connection before capturing.
        [[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] setVideoOrientation:[[(AVCaptureVideoPreviewLayer *)[[self previewView] layer] connection] videoOrientation]];

        // 5. Flash set to Auto for Still Capture
        [iSKITACamViewController setFlashMode:flashMode forDevice:[[self videoDeviceInput] device]];

        AVCaptureConnection *stillImageConnection = [[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo];

        CGFloat maxScale = stillImageConnection.videoMaxScaleAndCropFactor;
        if (effectiveScale > 1.0f && effectiveScale < maxScale)
        {
            stillImageConnection.videoScaleAndCropFactor = effectiveScale;;
        }

        [self playSound];
        // 6.Capture a still image.
        [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:[[self stillImageOutput] connectionWithMediaType:AVMediaTypeVideo] completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

            if (imageDataSampleBuffer)
            {
                NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                UIImage *image = [[UIImage alloc] initWithData:imageData];
                if(iPhone5 && [self.modeLabel.text isEqualToString:@"PhotoMode"]) {
                    UIImage *image1 = [image crop:CGRectMake(0, 0, image.size.width, image.size.height)];
                    [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image1 CGImage] orientation:(ALAssetOrientation)[image1 imageOrientation] completionBlock:nil];
                } else {
                    [[[ALAssetsLibrary alloc] init] writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:nil];

                }
                self.priveImageView.image = image;
            }
        }];
    });
}

这篇关于是否可以拍摄包括相机在内的组合视图的快照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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