繁体   English   中英

是否可以拍摄包括相机在内的组合视图的快照?

[英]Is it possible to take a snapshot of combined views including the camera?

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

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

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

“快照未呈现的视图将导致快照为空。请确保在快照之前至少对您的视图进行一次渲染或在屏幕更新后进行快照。”

有任何想法吗? 谢谢!

这是代码:

#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来实现。

 // 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];

==============================================我的倒计时快照功能:

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

暂无
暂无

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

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