gpt4 book ai didi

ios - 是否可以拍摄包括相机在内的组合 View 的快照?

转载 作者:行者123 更新时间:2023-11-29 12:19:26 25 4
gpt4 key购买 nike

我的应用程序有一个按钮,可以通过调用“takeSnapShot”(参见下面的代码)截取屏幕上的任何内容。我有两个 View ,其中一个是用于相机的选择器 View ,因此在屏幕上我看到来自相机的图像,旁边是另一个带有图像的 View 。

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

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

"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做的。

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

关于ios - 是否可以拍摄包括相机在内的组合 View 的快照?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31037769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com